我下载了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 from this post启用“Include prerelease”, 但是如何打开如何打开NuGet的“Include Prerelease”?
答案 0 :(得分:10)
如何打开如何打开NuGet的“Include Prerelease”?
要使用“Include Prerelease”安装Json.NET,请在the Package Manager Console
中运行以下命令Install-Package Newtonsoft.Json –IncludePrerelease
答案 1 :(得分:2)
另一个原因可能是您的GAC中有旧版本。对我来说,版本是在.NET 4.0 GAC中删除,这解决了问题
答案 2 :(得分: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.