2013-05-26 36 views
7

我下載了一個webapi的示例代碼,我得到這個異常 我打開這個示例使用Visual Studio 2012最終版本,我有最新的verion newtonsoft installed.I當我運行此應用程序時得到此錯誤。它成功編譯。 任何想法爲什麼這個錯誤,有沒有辦法解決這個異常。方法沒有找到'虛擬Newtonsoft.Json.Serialization.DefaultContractResolver.set_IgnoreSerializableAttribute(布爾)

System.MissingMethodException was unhandled by user code 
    HResult=-2146233069 
    Message=Method not found: 'Void Newtonsoft.Json.Serialization.DefaultContractResolver.set_IgnoreSerializableAttribute(Boolean)'. 
    Source=System.Net.Http.Formatting 
    StackTrace: 
     at System.Net.Http.Formatting.JsonContractResolver..ctor(MediaTypeFormatter formatter) 
     at System.Net.Http.Formatting.JsonMediaTypeFormatter..ctor() 
     at System.Net.Http.Formatting.MediaTypeFormatterCollection.CreateDefaultFormatters() 
     at System.Net.Http.Formatting.MediaTypeFormatterCollection..ctor() 
     at System.Web.Http.HttpConfiguration.DefaultFormatters() 
     at System.Web.Http.HttpConfiguration..ctor(HttpRouteCollection routes) 
     at System.Web.Http.GlobalConfiguration.<.cctor>b__0() 
     at System.Lazy`1.CreateValue() 
     at System.Lazy`1.LazyInitValue() 
     at System.Lazy`1.get_Value() 
     at System.Web.Http.GlobalConfiguration.get_Configuration() 
     at System.Web.Http.RouteCollectionExtensions.MapHttpRoute(RouteCollection routes, String name, String routeTemplate, Object defaults, Object constraints, HttpMessageHandler handler) 
     at System.Web.Http.RouteCollectionExtensions.MapHttpRoute(RouteCollection routes, String name, String routeTemplate, Object defaults) 
     at WebAPIRc.RouteConfig.RegisterRoutes(RouteCollection routes) in c:\Users\viemon\Downloads\WebAPIRc\WebAPIRc\WebAPIRc\App_Start\RouteConfig.cs:line 17 
     at WebAPIRc.WebApiApplication.Application_Start() in c:\Users\viemon\Downloads\WebAPIRc\WebAPIRc\WebAPIRc\Global.asax.cs:line 36 
    InnerException: 

這是我才知道,我需要打開「包含搶鮮」爲的NuGet from this post, 它失敗

public class RouteConfig 
    { 
     public static void RegisterRoutes(RouteCollection routes) 
     { 
      routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 
      //Exception error start 
      routes.MapHttpRoute(
       name: "DefaultApi", 
       routeTemplate: "api/{controller}/{id}", 
       defaults: new { id = RouteParameter.Optional } 
      ); 
      //exception error end 

      routes.MapRoute(
       name: "Default", 
       url: "{controller}/{action}/{id}", 
       defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
      ); 
     } 
    } 

的代碼,但我怎麼打開怎麼打開NuGet的「包括預售」?

回答

2

這樣做的另一個原因與以下命令「包含預發行」,運行安裝Json.NET可以,如果你在你的GAC舊版本。對我來說,版本是在.NET 4.0 GAC中,解決了這個問題

+0

你能提供關於這方面的更多細節嗎? –

+0

嘿,我把我的思想回來,但我想我有我的.NET4 Gac中的版本,可以在這裏確定http://stackoverflow.com/questions/6925043/where-is-gacutil-for-net-framework -4-0-windows-7,我必須刪除它,以便運行時可以使用新版本 https://msdn.microsoft.com/en-us/library/zykhfde0(v=vs.110 ).aspx 我不知道爲什麼該版本最初是在我的GAC中。希望有幫助 – PatrickWalker

1

在我的情況你我只是刪除RouteConfig中的以下行(它已經被刪除)。

//Exception error start 
    routes.MapHttpRoute(
     name: "DefaultApi", 
     routeTemplate: "api/{controller}/{id}", 
     defaults: new { id = RouteParameter.Optional } 
    ); 
    //exception error end 

在VS2012中,這應該足夠了。

 routes.MapRoute(
      name: "Default", 
      url: "{controller}/{action}/{id}", 
      defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
     ); 

您可能還需要驗證您的控制器。 也可以在RouteConfig中刪除一些Class引用,如果您不確定它們的用途。在我來說,我只保留了兩個:

using System.Web.Mvc; 
using System.Web.Routing; 

另一種方案是,如果你想,有一個時間來找出是什麼原因導致的衝突,那麼你可以將你的項目生成輸出的詳細程度來詳細(Go to Tools-> Options -> Project and Solutions -> Build and Run -> MSBuild project build output verbosity) and upon build check your logs.

+0

接受的答案不適用於我,但將其從RouteConfig中刪除。但是,你是否有任何文件指出刪除這不會導致任何問題? – Chad

相關問題