asp.net-mvc - 尝试通过方法 ... HttpConfiguration ... 访问方法 ... DefaultFormatters ... 失败

标签 asp.net-mvc

在进行一些项目重组后,我收到以下错误,但无法确定问题所在。

Server Error in '/' Application.
Attempt by method 'System.Web.Http.HttpConfiguration..ctor(System.Web.Http.HttpRouteCollection)' to access method 'System.Web.Http.HttpConfiguration.DefaultFormatters()' failed.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.MethodAccessException: Attempt by method 'System.Web.Http.HttpConfiguration..ctor(System.Web.Http.HttpRouteCollection)' to access method 'System.Web.Http.HttpConfiguration.DefaultFormatters()' failed.

Source Error:


Line 15:          routes.IgnoreRoute( "{resource}.axd/{*pathInfo}" );
Line 16: 
Line 17:          routes.MapHttpRoute(
Line 18:              name: "DefaultApi",
Line 19:              routeTemplate: "api/{controller}/{action}",


Source File: c:\Dev\MyProject\Branches\Phase.2.3-Sprint 16\Client\ManagementSite\App_Start\RouteConfig.cs    Line: 17

Stack Trace:


[MethodAccessException: Attempt by method 'System.Web.Http.HttpConfiguration..ctor(System.Web.Http.HttpRouteCollection)' to access method 'System.Web.Http.HttpConfiguration.DefaultFormatters()' failed.]
   System.Web.Http.HttpConfiguration..ctor(HttpRouteCollection routes) +53
   System.Web.Http.GlobalConfiguration.<.cctor>b__0() +54
   System.Lazy`1.CreateValue() +416
   System.Lazy`1.LazyInitValue() +152
   System.Lazy`1.get_Value() +75
   System.Web.Http.GlobalConfiguration.get_Configuration() +27
   System.Web.Http.RouteCollectionExtensions.MapHttpRoute(RouteCollection routes, String name, String routeTemplate, Object defaults, Object constraints, HttpMessageHandler handler) +67
   System.Web.Http.RouteCollectionExtensions.MapHttpRoute(RouteCollection routes, String name, String routeTemplate, Object defaults) +18
   MyProjectManagementSite.RouteConfig.RegisterRoutes(RouteCollection routes) in c:\Dev\MyProject\Branches\Phase.2.3-Sprint 16\Client\ManagementSite\App_Start\RouteConfig.cs:17
   MyProjectManagementSite.MvcApplication.Application_Start() in c:\Dev\MyProject\Branches\Phase.2.3-Sprint 16\Client\ManagementSite\Global.asax.cs:26

[HttpException (0x80004005): Attempt by method 'System.Web.Http.HttpConfiguration..ctor(System.Web.Http.HttpRouteCollection)' to access method 'System.Web.Http.HttpConfiguration.DefaultFormatters()' failed.]
   System.Web.HttpApplicationFactory.EnsureAppStartCalledForIntegratedMode(HttpContext context, HttpApplication app) +9905705
   System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers) +118
   System.Web.HttpApplication.InitSpecial(HttpApplicationState state, MethodInfo[] handlers, IntPtr appContext, HttpContext context) +172
   System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext, HttpContext context) +336
   System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext) +296

[HttpException (0x80004005): Attempt by method 'System.Web.Http.HttpConfiguration..ctor(System.Web.Http.HttpRouteCollection)' to access method 'System.Web.Http.HttpConfiguration.DefaultFormatters()' failed.]
   System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +9885060
   System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +101
   System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +254


Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.34009 

我将所有项目都绑定(bind)到 .Net 4.5,并将 Newtonsoft Json.Net 全面设置为 6.0.1(而不是最新的 6.0.7)。当我构建 Web 项目时,我收到一条警告“发现同一依赖程序集的不同版本之间存在冲突”,如果我运行一些借用的代码来列出这些潜在冲突,则 Newtonsoft 的 4.5 和 6.0 版本之间可能存在冲突:

Possible conflicts for System.Net.Http:
System.Net.Http.Formatting references System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
System.ServiceModel       references System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
System.Web.Http           references System.Net.Http, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
System.Web.Http.WebHost   references System.Net.Http, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
MyProjectManagementSite  references System.Net.Http, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
MyProjectManagementSite  references System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a

Possible conflicts for Newtonsoft.Json:
System.Net.Http.Formatting references Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed
System.Web.Http           references Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed
MyProject.Base           references Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed
MyProjectManagementSite  references Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed

以上是否显示了可能的原因?我该如何解决它?

最佳答案

卸载 WebApi 并再次重新安装(通过 Nuget),问题就消失了

关于asp.net-mvc - 尝试通过方法 ... HttpConfiguration ... 访问方法 ... DefaultFormatters ... 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27780404/

相关文章:

c# - 用于获取刷新 token 的 Google C# 客户端库

jquery - 处理 jquery ui 对话框中表单中的错误

asp.net-mvc - HttpClient传递多个简单参数

asp.net-mvc - 保存对 CheckBox 更改的更改?

c# - ??有 2 个字符串的运算符?

c# - Entity Framework 上的 Razor 类验证

asp.net-mvc - Visual Studio 2013 MVC 模板在与 Unity 一起使用时引发错误

asp.net-mvc - 在ASP.NET MVC中封装用户控件

c# - ASP.Net MVC3 通过或不通过 Visual Studio 修改日期和小数的文化?

asp.net-mvc - 使用 ActionFilterAttribute 测试 MVC Controller 操作