【问题标题】:Invalid OData path and Invalid action error无效的 OData 路径和无效的操作错误
【发布时间】:2014-05-28 08:45:26
【问题描述】:

我正在尝试创建一个 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 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)"}

我错过了什么?

非常感谢任何帮助。

问候。

【问题讨论】:

    标签: asp.net-mvc-5 asp.net-web-api2


    【解决方案1】:

    今天刚吃完。

    改变

    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

    【讨论】:

      猜你喜欢
      • 2023-03-04
      • 1970-01-01
      • 2018-03-05
      • 1970-01-01
      • 2018-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多