ASP.NET WebAPI OData - 继承自EntitySetController <>但使用Get(ODataQueryOptions选项)而不是[Queryable] Get()

Ian*_*tes 6 odata asp.net-web-api

我正在使用ASP.Net WebAPI每晚构建(2013-01-16)以获得最新的OData支持.

正如MSDN OData 0.2.0-alpha发布帖上Meta-Me博客所说,现在EntitySetController<T>可以从中派生OData控制器来消除很多痛苦和管道代码.

EntitySetController<T>类实现获取()作为

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

我想利用Get(ODataQueryOptions options)ASP.Net Web API OData支持提供的更具体的方法.

我把它编码为

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();
}
Run Code Online (Sandbox Code Playgroud)

(我也有这个返回IQueryable <>并看到其他人谈论ODataResult - 这是我目前无法发现的类型).

但是,如果我尝试在自己的控制器中使用基于ODataQueryOptions的Get方法,则会收到有关与请求匹配的多个操作的错误消息.特别是那个错误

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]]
Run Code Online (Sandbox Code Playgroud)

我假设这是由于路由解析器(抱歉,如果这是糟糕的ASP.NET路由术语)在控制器的基类以及控制器类本身上看到Get()或Get(...).

问题:a)有没有办法调整路线来解决这个问题?b)如果没有,我应该制作自己的版本EntitySetController<T>并换掉它的Get()方法吗?

Application_Start()调用的配置仅限于

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();

}
Run Code Online (Sandbox Code Playgroud)

没有其他配置被调用来处理路由或处理程序等.请注意,根据 CodePlex讨论,HttpConfiguration上的EnableOData()方法不再存在于最新的每晚构建中.

非常感谢!

You*_*oui 13

很高兴看到你正在使用我们的夜间构建:)

您获得多个匹配操作错误的原因是因为EntitySetController已经定义了一个Get方法.好消息是EntitySetController还定义了一个QueryOptions可用于检索查询选项的属性.因此,您应该能够覆盖EntitySetController的Get方法,并使用查询选项属性而不是参数.它的行为应该与将查询选项绑定到action参数的方式完全相同.