10

で説明されている RateProduct アクションに近いものを実現したい: http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/odata-actions

そのチュートリアルでは、次のように定義されています。

[HttpPost]
public int RateProduct([FromODataUri] int key, ODataActionParameters parameters) 
{ 
    // ...
}

ODataModelBuilder modelBuilder = new ODataConventionModelBuilder();
modelBuilder.EntitySet<Product>("Products");

// New Code
ActionConfiguration rateProduct = modelBuilder.Entity<Product>().Action("RateProduct");
rateProduct.Parameter<int>("Rating");
rateProduct.Returns<int>();

ただし、特定の半径内にある他の Location を返すのに十分なほどスマートな Location エンティティの使用例があります。おおよそ次のようになります。

[HttpPost]
public IQueryable<Location> GetLocationsWithinRadius([FromODataUri] int key, ODataActionParameters parameters) 
{ 
    // Get the Location instance intended to be the center of the radius by using the key
    // Do a radius search around it using (int)parameters["radius"] as the radius
    // return the IQueryable<Location> of all location found within that radius
}

ODataModelBuilder modelBuilder = new ODataConventionModelBuilder();
modelBuilder.EntitySet<Location>("Locations");

// New Code
ActionConfiguration getLocations = modelBuilder.Entity<Location>().Action("GetLocationsWithinRadius");
getLocations.Parameter<int>("radius");
getLocations.Returns<IQueryable<Location>>();

これを機能させたいと思っていますが、現在、戻り値の型がIQueryable<Location>. 戻り値の型が int のようなプリミティブである場合は機能しますが、それ以外の場合は、フィドラーで投稿を作成すると次のエラーが発生します (投稿は次のようなものhttp://localhost:2663/odata/Locations(2112)/GetLocationsWithinRadiusで、要求本文は です{radius: 50})。

{
  "odata.error":{
    "code":"","message":{
      "lang":"en-US","value":"An error has occurred."
    },"innererror":{
      "message":"The 'ObjectContent`1' type failed to serialize the response body for content type 'application/json; odata=minimalmetadata; streaming=true; charset=utf-8'.","type":"System.InvalidOperationException","stacktrace":"","internalexception":{
        "message":"The related entity set could not be found from the OData path. The related entity set is required to serialize the payload.","type":"System.Runtime.Serialization.SerializationException","stacktrace":"   at System.Web.Http.OData.Formatter.Serialization.ODataFeedSerializer.WriteObject(Object graph, ODataMessageWriter messageWriter, ODataSerializerContext writeContext)\r\n   at System.Web.Http.OData.Formatter.ODataMediaTypeFormatter.<>c__DisplayClassa.<WriteToStreamAsync>b__9()\r\n   at System.Threading.Tasks.TaskHelpers.RunSynchronously(Action action, CancellationToken token)"
      }
    }
  }
}

私が達成しようとしていることを行うことは可能ですか? もしそうなら、返さIQueryable<Location>れたものがodataパラメーターで構成可能になるかどうかをあえて尋ねます...(それはいいでしょう)?

ありがとう

4

1 に答える 1

20

Looks like your action configuration is incorrect. Try the following and see if it works:

//getLocations.Returns<IQueryable<Location>>();
getLocations.ReturnsCollectionFromEntitySet<Location>("Locations");
于 2013-06-17T21:01:51.770 に答える