2014-10-08 17 views
10
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'

'System.Web.Http.HttpConfiguration' does not contain a definition for 'EnableQuerySupport' and no extension method 'EnableQuerySupport' accepting a first argument of type 'System.Web.Http.HttpConfiguration' could be found (are you missing a using directive or an assembly reference?)

+0

我已经检查system.web.http的版本和它的正确。 – user3705566 2014-10-08 02:16:57

回答

0

检查System.Web.Http.OData.dll - 这是其中这些方法是从哪里来的,根据MSDN

+1

好吧现在它已经消失了。谢谢。但我收到类似的错误“EnableSystemDiagnosticsTracing”,请让我知道可能是什么问题。 – user3705566 2014-10-08 02:35:06

+0

请注意,此方法现在已过时。 https://msdn.microsoft.com/en-us/library/system.web.http.odata.enablequeryattribute(v=vs.118).aspx – 2017-03-13 11:14:47

9

在Visual Studio中转到“工具”,然后“NuGet包管理器”,然后选择“包管理器控制台”

运行下面的命令,并愿买电子健康我解决了这个问题。已经测试过它。

更新包microsoft.aspnet.webapi -reinstall