我正在使用ASP.Net WebAPI每晚构建(2013-01-16)以获得最新的OData支持。
正如Meta-Me blog on MSDN OData 0.2.0-alpha release post所说,现在有一个EntitySetController<T>
可以从中导出OData控制器,以消除很多痛苦和管道代码。
EntitySetController<T>
类将Get()实现为
[Queryable]
public virtual IQueryable<TEntity> Get()
{
throw EntitySetControllerHelpers.GetNotImplementedResponse(Request);
}
我想利用ASP.Net Web API OData支持提供的更具体的Get(ODataQueryOptions options)
方法。
我把它编码为
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;&gt;并看到其他人谈论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]]
我认为这是由于路由解析器(对不起,如果这是糟糕的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();
}
没有其他配置被调用来处理路由或处理程序等。请注意,根据this CodePlex讨论,HttpConfiguration上的EnableOData()方法不再存在于最新的每晚构建中。
非常感谢!
答案 0 :(得分:13)
看到你正在使用我们的夜间版本非常酷:)
您获得多个匹配操作错误的原因是因为EntitySetController已经定义了一个Get方法。好消息是EntitySetController还定义了一个QueryOptions
属性,可用于检索查询选项。因此,您应该能够覆盖EntitySetController的Get方法,并使用查询选项属性而不是参数。它的行为应与将查询选项绑定到操作参数的方式完全相同。