【发布时间】:2014-09-17 15:37:21
【问题描述】:
我正在尝试让我的 Odata 端点执行一个操作,该操作返回与它所在的控制器相同的 DataType。
我在我的 WebApiConfig 中尝试了以下操作:
var entity = builder.EntitySet<Entity>("entities");
builder.Entity<Entity>().Action("UnassignedMarkets").ReturnsCollection<Entity>();
我收到以下错误
System.Web.Http.OData.dll 中出现“System.InvalidOperationException”类型的异常,但未在用户代码中处理 附加信息:EDM 类型“MyProj.DataAccess.Views.Entity”已声明为实体类型。如果返回类型是实体集合,请使用方法“ReturnsCollectionFromEntitySet”。
所以我将我的代码更改为以下代码并编译
var entity = builder.EntitySet<Entity>("entities");
builder.Entity<Entity>().Action("UnassignedMarkets").ReturnsCollectionFromEntitySet<Entity>("entities");
我使用 fiddler 浏览到操作
http://localhost:777/odata/entities/UnassignedMarkets
返回:
{ "message": "OData 路径无效。", "exceptionMessage": "检测到无效操作。'UnassignedMarkets' 不是可以绑定到的操作 'Collection([MyProj.DataAccess.Dtos.Views.Entity Nullable=False])'.", "exceptionType": "Microsoft.Data.OData.ODataException", "stackTrace": "在 System.Web.Http.OData.Routing.DefaultODataPathHandler.ParseAtEntityCollection(IEdmModel 模型、ODataPathSegment 以前的、IEdmType 以前的EdmType、字符串 段)\r\n 在 System.Web.Http.OData.Routing.DefaultODataPathHandler.ParseAtCollection(IEdmModel 模型、ODataPathSegment 以前的、IEdmType 以前的EdmType、字符串 段)\r\n 在 System.Web.Http.OData.Routing.DefaultODataPathHandler.ParseNextSegment(IEdmModel 模型、ODataPathSegment 以前的、IEdmType 以前的EdmType、字符串 段)\r\n 在 System.Web.Http.OData.Routing.DefaultODataPathHandler.Parse(IEdmModel 模型,字符串 odataPath)\r\n at System.Web.Http.OData.Routing.ODataPathRouteConstraint.Match(HttpRequestMessage request, IHttpRoute route, String parameterName, IDictionary`2 values, HttpRouteDirection routeDirection)" }
public class EntitiesController : ODataController
{
private readonly Storage _storage;
public EntitiesController(Storage storage)
{
_storage = storage;
}
[Queryable]
public IQueryable<Entity> Get()
{
return _storage.Entities;
}
[HttpPost]
public IQueryable<Entity> UnassignedMarkets(ODataQueryOptions<Entity> queryOptions)
{
return buildEntities();
}
}
【问题讨论】:
标签: asp.net-web-api odata