我正在尝试创建一个ODataController并让它像这样:
public class ProductSetController : ODataController
{
public IQueryable<ProductRef> GetAllProducts(string StoreId, string flag)
{
long lStoreId = Convert.ToInt64(StoreId);
var featuredProducts = (from b in new SomeContext().SomeInfluence
where b.SomeInfluenceTypeId == 1234 && b.StoreId == lStoreId && b.IsDeleted == false
select b.ProductId).ToList();
return (from b in new OProdctSubscriptionContext(lStoreId).ProductRef where featuredProducts.Contains(b.ProductId) select b).AsQueryable();
}
}
这是在WebApiConfig的Register方法中:
ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<ProductRef>("ProductSet");
config.Routes.MapODataRoute("ODataRoute", "odata", builder.GetEdmModel());
为了测试它,我打了一个电话:
http://localhost:32944/odata/ProductSet/GetAllProducts?StoreId=5010&flag="N"
浏览器打印出此错误:
{“$ id”:“1”,“Message”:“OData路径是 无效。“,”ExceptionMessage“:”检测到无效的操作。 'GetAllProducts'不是可绑定的动作 “类别([Entities.OpenApi.Products.ProductRef 可空=假])” “” ExceptionType。 “:” Microsoft.Data.OData.ODataException “ ”堆栈跟踪“:” 在 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)“}
我错过了什么?
非常感谢任何帮助。
问候。
答案 0 :(得分:3)
今天就这样了。
更改
builder.EntitySet<ProductRef>("ProductSet");
为:
builder.EntitySet<ProductRef>().Collection.Action("GetAllProducts");
添加
[HttpPost]
public IQueryable<ProductRef> GetAllProducts(string StoreId, string flag)
此页面帮助了我:http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/odata-actions