Method not found 'Void Newtonsoft.Json.Serialization.Def

本文关键字:Newtonsoft Json Serialization Def Void not found Method | 更新日期: 2023-09-27 18:15:28

我下载了webapi的示例代码,我得到了这个异常我使用Visual Studio 2012 Ultimate Version打开这个示例,并安装了最新版本的newtonsoft。当我运行这个应用程序时,我得到这个错误。编译成功。你知道为什么会出现这个错误吗?有没有办法解决这个异常?

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: 

下面是失败的代码

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打开"包括预发布",但是我如何打开如何打开NuGet的Include预发布

Method not found 'Void Newtonsoft.Json.Serialization.Def

如何打开NuGet的"Include Prerelease"

安装Json。. NET使用"Include Prerelease",在包管理器控制台

中运行以下命令
Install-Package Newtonsoft.Json –IncludePrerelease

另一个原因可能是您的GAC中有较旧的版本。对我来说,这个版本是在。net 4.0 GAC中,删除这个解决了这个问题

在我的例子中,我只是删除了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,如果你不确定他们是什么。在我的例子中,我只保留了这两个:

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.