我想实现接近于 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 实体的用例,它足够聪明,可以在它周围的某个半径范围内返回其他位置。大致应该是这样的:
[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 这样的原始类型,那么它可以工作,否则当我在 fiddler 中创建帖子时它会给出以下错误(帖子类似于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 参数组合......(那会很好)?
谢谢