我正试图让我的Odata enpoint有一个动作,它返回与它所在的控制器相同的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) model,ODataPathSegment previous,IEdmType previousEdmType,String 段)\ r \ n at System.Web.Http.OData.Routing.DefaultODataPathHandler.ParseAtCollection(IEdmModel model,ODataPathSegment previous,IEdmType previousEdmType,String 段)\ r \ n at System.Web.Http.OData.Routing.DefaultODataPathHandler.ParseNextSegment(IEdmModel model,ODataPathSegment previous,IEdmType previousEdmType,String 段)\ r \ n at System.Web.Http.OData.Routing.DefaultODataPathHandler.Parse(IEdmModel model,String 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();
}
}
答案 0 :(得分:0)
根据您的要求:
http://localhost:777/odata/entities/UnassignedMarkets
行动&#34; UnassignedMarkets&#34;必然是实体的集合。
因此,您可以尝试以下代码来构建模型:
var entity = builder.EntitySet<Entity>("entities");
builder.Entity<Entity>().Collection.Action("UnassignedMarkets").ReturnsCollectionFromEntitySet<Entity>("entities");