.NET的WebAPI的OData操作返回一个可查询 [英] .Net WebApi OData Actions that return an Queryable

查看:349
本文介绍了.NET的WebAPI的OData操作返回一个可查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现的东西接近描述的RateProduct动作:<一href=\"http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/odata-actions\">http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/odata-actions

I want to achieve something close to the RateProduct action described in: 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>();

不过,我有足够聪明,它周围一定半径内返回其他位置一个位置的实体的用例。它应该大致是这样的:

However, I have the use case of a Location entity that is smart enough to return other Locations within a certain radius around it. It should roughly be like this:

[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&LT;地点&gt; 。如果返回类型是基本像一个int,那么它的工作原理,否则当我创建的提琴手后提供了以下错误(后有点像的http://本地主机:2663 / ODATA /位置(2112)/ GetLocationsWithinRadius 和请求主体 {半径:50}

I would love to get this to work and currently it doesn't work when the the return type is an IQueryable<Location>. If the return type is a primitive like an int, then it works, otherwise it gives the following error when I create a post in fiddler (the post is something like http://localhost:2663/odata/Locations(2112)/GetLocationsWithinRadius and the Request Body is {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&LT;地点&gt; 变得可组合使用ODATA PARAMATERS ...(这将是很好)

Is it possible to do what I am trying to accomplish? And if it is, dare I ask if the returned IQueryable<Location> becomes composable with odata paramaters...(that would be nice)?

感谢

推荐答案

看起来像你的动作配置不正确。请尝试以下方法,看看它的工作原理:

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

//getLocations.Returns<IQueryable<Location>>();
getLocations.ReturnsCollectionFromEntitySet<Location>("Locations");

这篇关于.NET的WebAPI的OData操作返回一个可查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆