ASP.NET的WebAPI的OData - 从EntitySetController&LT继承;>但使用GET(ODataQueryOptions选项),而不是[可查询]得到() [英] ASP.NET WebAPI OData - Inheriting from EntitySetController<> but using Get(ODataQueryOptions options) rather than [Queryable]Get()

查看:166
本文介绍了ASP.NET的WebAPI的OData - 从EntitySetController&LT继承;>但使用GET(ODataQueryOptions选项),而不是[可查询]得到()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是ASP.Net的WebAPI夜间生成(2013年1月16日),以获得最新的OData支持可能的。

I'm using the ASP.Net WebAPI nightly build (2013-01-16) to get the latest OData support possible.

随着<一个href=\"http://blogs.msdn.com/b/alexj/archive/2012/11/02/odata-in-webapi-microsoft-asp-net-web-api-odata-0-2-0-alpha-release.aspx\">Meta-Me MSDN上的OData 0.2.0-alpha版本后说博客,现在有一个 EntitySetController&LT; T&GT; 从OData的控制器可以得出带走了很多疼痛和管道code。

As the Meta-Me blog on MSDN OData 0.2.0-alpha release post says, there's now an EntitySetController<T> from which OData controllers can be derived to take away a lot of the pain and plumbing code.

EntitySetController&LT; T&GT; 类实现获取()为

[Queryable]
public virtual IQueryable<TEntity> Get()
{
    throw EntitySetControllerHelpers.GetNotImplementedResponse(Request);
}

我想使用的ASP.Net的Web API的OData支持提供了更具体的获取(ODataQueryOptions选项)方法。

我有codeD为

public IEnumerable<Patient> Get(ODataQueryOptions options)
{
    IQueryable patients = entities.Patients;

    if (options.Filter != null)
    {
        patients = options.Filter.ApplyTo(patients, new ODataQuerySettings());
    }

    return (patients as IQueryable<Patient>).AsEnumerable();
}

(我也有这个返回的IQueryable&LT;>,看见别人讲一个ODataResult - 这是一个类型的,我不能在此刻发现)。

(I've also had this return IQueryable<> and saw someone else talk about an ODataResult - that's a type I can't discover at the moment).

不过,如果我尝试使用基于ODataQueryOptions-Get方法在我自己的控制器我得到的匹配要求多个动作的错误消息。具体的误差

However, if I try to use the ODataQueryOptions-based Get method in my own controller I get an error message about multiple actions matching the request. Specifically that error is

Multiple actions were found that match the request: 

System.Collections.Generic.IEnumerable`1[Dox.Server.Model.Patient] Get(System.Web.Http.OData.Query.ODataQueryOptions) on type Dox.Server.Web.Controllers.PatientController

System.Linq.IQueryable`1[Dox.Server.Model.Patient] Get() on type System.Web.Http.OData.EntitySetController`2[[Dox.Server.Model.Patient, Dox.Server.Model, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[System.Guid, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]

我想这是由于路径解析(抱歉,如果这是可怜的ASP.NET路由的术语)上看到控制器的基类的get()或获取(...),以及控制器类本身。

I assume this is due to the route resolver (sorry if that's poor ASP.NET routing terminology) seeing Get() or Get(...) on the controller's base class as well as the controller class itself.

问题:
a)是否有调整的路线来解决这个问题的一些方法?
b)若没有,我应该让我自己的版本 EntitySetController&LT; T&GT; 和换出它的get()方法

Questions: a) Is there some way of adjusting routes to fix this? b) If not, should I make my own version of EntitySetController<T> and swap out it's Get() method?

通过的Application_Start()被调用时的配置是有限的,以

The configuration being called by Application_Start() is limited to

public static void EnableOData( HttpConfiguration config )
{
    var model = BuildModelImplicitly(config);

    //As per LinqPad forum: http://forum.linqpad.net/discussion/178/odata-v3-not-working
    IEdmEntityContainer container = model.EntityContainers().First();
    model.SetIsDefaultEntityContainer(container, true);

    //config.EnableOData(model, "api");
    config.Routes.MapODataRoute("OData", "api", model);

    //config.EnableSystemDiagnosticsTracing();

}

有没有其他的配置被称为与路线或处理程序等做注意在HttpConfiguration最新不再存在EnableOData()方法,每晚构建按的这个 codePLEX讨论。

There's no other configuration being called to do with routes or handlers, etc. Note that the EnableOData() method on HttpConfiguration no longer exists in the latest nightly builds as per this CodePlex discussion.

非常感谢!

推荐答案

这是非常冷静地看到,您使用的是每晚构建:)

It's very cool to see that you're using our nightly builds :)

你得到多个匹配的操作错误的原因是因为EntitySetController已经定义了一个GET方法。好消息是,EntitySetController还定义了一个 QueryOptions,你可以用它来检索查询选项属性。所以,你应该能够覆盖EntitySetController的get方法,并使用查询选项属性,而不是参数。它应该表现得完全一样,如果你已经绑定的查询选项到操作参数。

The reason you're getting a multiple matching actions error is because EntitySetController already defines a Get method. The good news is that EntitySetController also defines a QueryOptions property that you can use to retrieve the query options. So you should be able to override the EntitySetController's Get method and use the query options property instead of the parameter. It should behave exactly the same way as if you had bound the query options to an action parameter.

这篇关于ASP.NET的WebAPI的OData - 从EntitySetController&LT继承;&GT;但使用GET(ODataQueryOptions选项),而不是[可查询]得到()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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