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

11
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'的第一个参数(是否缺少using指令或程序集引用?)


我已经检查了 system.web.http 的版本,它是正确的。 - user3705566
3个回答

10
在Visual Studio中,转到 "工具",然后转到 "Nuget程序包管理器",再点击 "程序包管理器控制台"。
运行以下命令,它将解决问题。已经进行了测试。 update-package microsoft.aspnet.webapi -reinstall

2

请使用以下链接: https://learn.microsoft.com/zh-cn/dotnet/api/microsoft.aspnet.odata.extensions.httpconfigurationextensions.addodataqueryfilter?view=odata-aspnet-7.0 - ozz

0

检查 System.Web.Http.OData.dll - 根据 MSDN,这个方法是从这里来的。


1
好的,现在它已经消失了。谢谢你。但我对“EnableSystemDiagnosticsTracing”也收到了类似的错误,请告诉我可能是什么问题。 - user3705566
请注意,此方法现已过时。https://msdn.microsoft.com/zh-cn/library/system.web.http.odata.enablequeryattribute(v=vs.118).aspx - Hugo Nava Kopp

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接