ASP.NET Web API error after upgrading to Visual St

2019-01-26 05:37发布

After upgrading to Visual Studio 2012 I can no longer access any of my ApiControllers, the following error is thrown:

Server Error in '/' Application.

Method not found: 'Void System.Net.Http.Headers.HttpHeaders.AddWithoutValidation(System.String, System.Collections.Generic.IEnumerable`1<System.String>)'.

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.MissingMethodException: Method not found: 'Void System.Net.Http.Headers.HttpHeaders.AddWithoutValidation(System.String, System.Collections.Generic.IEnumerable`1<System.String>)'.

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace: 


[MissingMethodException: Method not found: 'Void System.Net.Http.Headers.HttpHeaders.AddWithoutValidation(System.String, System.Collections.Generic.IEnumerable`1<System.String>)'.]
   System.Web.Http.WebHost.HttpControllerHandler.AddHeaderToHttpRequestMessage(HttpRequestMessage httpRequestMessage, String headerName, String[] headerValues) +0
   System.Web.Http.WebHost.HttpControllerHandler.ConvertRequest(HttpContextBase httpContextBase) +248
   System.Web.Http.WebHost.HttpControllerHandler.BeginProcessRequest(HttpContextBase httpContextBase, AsyncCallback callback, Object state) +79
   System.Web.Http.WebHost.HttpControllerHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) +48
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +268
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

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

Anyone knows how to fix this?

4条回答
你好瞎i
2楼-- · 2019-01-26 06:22

You have a reference to an old version of System.Net.Http in your project. To fix this, go under "References" in your project, delete System.Net.Http, and add the version that comes with .NET 4.5 instead. That should do it.

Henrik

查看更多
走好不送
3楼-- · 2019-01-26 06:34

step 1 Remove the below references from your project:

System.Net.Http, System.Web.Http, System.Web.Http.WebHost , System.Web.Http.Common & System.Net.Http.Formatting

step 2 Add the above refefrences (except the System.Web.Http.Common & System.Net.Http.Formatting) from Below location

  1. C:\Program Files (x86)\Microsoft ASP.NET\ASP.NET MVC 4\Packages\Microsoft.AspNet.WebApi.Core.4.0.20505.0\lib\net40

  2. C:\Program Files (x86)\Microsoft ASP.NET\ASP.NET MVC 4\Packages\Microsoft.AspNet.WebApi.WebHost.4.0.20505.0\lib\net40

  3. C:\Program Files (x86)\Microsoft ASP.NET\ASP.NET MVC 4\Packages\Microsoft.Net.Http.2.0.20505.0\lib\net40

and add one more new dll Newtonsoft.Json.dll from

  1. C:\Program Files (x86)\Microsoft ASP.NET\ASP.NET MVC 4\Packages\Newtonsoft.Json.4.5.1\lib\net40
查看更多
时光不老,我们不散
4楼-- · 2019-01-26 06:37

Solution to this and other RC-related issues right here.

查看更多
我欲成王,谁敢阻挡
5楼-- · 2019-01-26 06:40

This fixed it for me.

http://forums.asp.net/t/1809919.aspx/1

Method not found: 'Void System.Net.Http.Headers.HttpHeaders.AddWithoutValidation(System.String, System.Collections.Generic.IEnumerable`1)'.

I resolved it with the following steps. Not sure if all of them are required, but it worked.

In NuGet Package Manager, uninstalled the Web.API Beta package. Added references to System.Net.Http, System.Web.Http, & System.Web.Http.WebHost (these were removed by the previous step). Installed Json.Net via NuGet.

As an addition, if your project is targeting the 4.0 version of the framework, you must include the new Microsoft ASP.Net Web API set of packages from NUGET, to get a green light for your build. Json.Net is a dependency for these new packages, so you need not install it separate.

查看更多
登录 后发表回答