简体   繁体   English

在 WebApi 2 ASP.NET 4.6.1 上启用 OData 过滤

[英]Enabling OData Filtering on WebApi 2 ASP.NET 4.6.1

I have a legacy WebApi running on ASP.NET 4.6.1 where I need to be able to enable OData fitler support to an endpoint.我有一个在 ASP.NET 4.6.1上运行的旧版 WebApi,我需要能够启用对端点的 OData fitler 支持。

However, when debugging the controller through Postman, the ODataQueryOptions<Job> searchOptions on the controller is always null. However, when debugging the controller through Postman, the ODataQueryOptions<Job> searchOptions on the controller is always null.

My query string example is;我的查询字符串示例是;

https://localhost:44310/api/job/find/odata?Filter=[["Title"%2C"contains"%2C"T"]]&Skip=1&Top=5

I have added System.Web.Http.OData - V5.7.0.0 reference to the controller.我添加了System.Web.Http.OData - V5.7.0.0对 controller 的引用。

With the following in my WebApiConfig Register method;在我的WebApiConfig Register方法中使用以下内容;

    public static void Register(HttpConfiguration config)
    {
        // Web API configuration and services

        // Web API routes
        config.MapHttpAttributeRoutes();

        config.EnableQuerySupport();
        //config.AddODataQueryFilter();

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

    }

To note I have tried both EnableQuerySupport() which intellisense is saying is obselete and AddODataQueryFilter() however neither seems to work.需要注意的是,我已经尝试过 Intellisense 所说的 EnableQuerySupport( EnableQuerySupport()AddODataQueryFilter()但似乎都不起作用。

In my controller I then have the following code;在我的 controller 中,我有以下代码;

    [HttpGet]
    [EnableQuery]
    [Route("find/odata")]
    public PageResult<Job> Find(ODataQueryOptions<Job> searchOptions)
    {
        ...
        return new PageResult<Job>(response.Result, null, null);
    }

I have tried with and without the [EnableQuery] filter with no change.我尝试使用和不使用[EnableQuery]过滤器,没有任何变化。

for reference the link I have been using is;作为参考,我一直在使用的链接是;

Microsoft OData tutorial 微软 OData 教程

Any resolution on this?对此有何解决方案? I'm seeing the same behavior.我看到了同样的行为。 If I globally enable filter via config.Filter(), it works, but I'd rather enable it on a class by class or property by property basic, which doesn't appear to work.如果我通过 config.Filter() 全局启用过滤器,它可以工作,但我宁愿在 class 上由 class 或按属性基本属性启用它,这似乎不起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM