“System.Web.Http.HttpConfiguration”不包含“EnableQuerySupport”的定

本文关键字:包含 EnableQuerySupport 的定 HttpConfiguration System Web Http | 更新日期: 2023-09-27 18:25:49

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using InCubatize.Helpers;
namespace InCubatize
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            //1
            ////Create and instance of TokenInspector setting the default inner handler
            //TokenInspector tokenInspector = new TokenInspector() { InnerHandler = new HttpControllerDispatcher(config) };
            ////Just exclude the users controllers from need to provide valid token, so they could authenticate
            //config.Routes.MapHttpRoute(
            //    name: "Authentication",
            //    routeTemplate: "api/users/{id}",
            //    defaults: new { controller = "users" }
            //);
            //config.Routes.MapHttpRoute(
            //    name: "DefaultApi",
            //    routeTemplate: "api/{controller}/{id}",
            //    defaults: new { id = RouteParameter.Optional },
            //    constraints: null,
            //    handler: tokenInspector
            //);
            //end1

            config.Routes.MapHttpRoute(name: "DefaultApiWithAction",
                           routeTemplate: "api/{controller}/{action}/{id}",
                           defaults: new { id = RouteParameter.Optional });
            config.Routes.MapHttpRoute(name: "DefaultApiWithActionAndTwoParams",
                        routeTemplate: "api/{controller}/{action}/{id1}/{id2}",
                        defaults: new { id = RouteParameter.Optional });
            config.Routes.MapHttpRoute(name: "DefaultApiWithActionAndFiveParams",
                                    routeTemplate: "api/{controller}/{action}/{id1}/{id2}/{id3}/{id4}/{id5}/{id6}",
                                    defaults: new { id = RouteParameter.Optional });
            //Old Code.
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
            config.EnableQuerySupport();
            // To disable tracing in your application, please comment out or remove the following line of code
            // For more information, refer to: http://www.asp.net/web-api

            var json = config.Formatters.JsonFormatter;
            json.SerializerSettings.PreserveReferencesHandling =
                Newtonsoft.Json.PreserveReferencesHandling.None;
            config.Formatters.Remove(config.Formatters.XmlFormatter);
            config.EnableSystemDiagnosticsTracing();
        }
    }
}

得到以下错误:-

"System.Web.Http.HttpConfiguration"不包含"EnableQuerySupport"的定义,也没有扩展方法"EnableQuerySupport" 接受类型为"System.Web.Http.HttpConfiguration"的第一个参数 可以找到(您是否缺少使用指令或程序集 参考?

“System.Web.Http.HttpConfiguration”不包含“EnableQuerySupport”的定

在Visual Studio中,转到"Tools",然后转到"Nuget Package Manager",然后转到"Package Manager Console"

运行以下命令,它将解决问题。已经测试过了。

update-package microsoft.aspnet.webapi -reinstall

注意:此 API 现已过时。

https://msdn.microsoft.com/de-de/library/jj918506(v=vs.118(.aspx

Check System.Web.Http.OData.dll - 根据MSDN,这就是这种方法的来源