Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.net Web API and System.Net.Http

Following the example in this article: http://blogs.msdn.com/b/yaohuang1/archive/2012/05/21/asp-net-web-api-generating-a-web-api-help-page-using-apiexplorer.aspx

I've set up everything to provide documentation for my Web API project but I'm running into an issue. When I try to use @api.HttpMethod I get the error he describes about halfway through the article. He says you have to manually add a reference to the System.Net.Http, Version=2.0.0.0 assembly in the web.config (even though it's in the References folder by default), but if you follow his example of adding the assembly the traditional way through the tag in web.config..... well you'll find that is no longer a valid tag in 4.5 and everything is done through AssemblyRedirects. I tried that but to no avail. Anyone having this issue or know how to help with the change in web.config? Did I miss a meeting?

Visual Studio 2012 MVC4 Web API project (not from Nuget, the final release shipped with VS2012)

like image 431
user1741945 Avatar asked Oct 12 '12 19:10

user1741945


People also ask

What is the difference between ASP.NET and Web API?

Asp.Net MVC is used to create web applications that return both views and data but Asp.Net Web API is used to create full-blown HTTP services with an easy and simple way that returns only data, not view. Web API helps to build REST-ful services over the .

What is System NET HTTP?

Provides a programming interface for modern HTTP applications, including HTTP client components that allow applications to consume web services over HTTP and HTTP components that can be used by both clients and servers for parsing HTTP headers. Commonly Used Types: System. Net.

Is RestSharp better than HttpClient?

The main conclusion is that one is not better than the other, and we shouldn't compare them since RestSharp is a wrapper around HttpClient. The decision between using one of the two tools depends on the use case and the situation.

Can I use Web API with ASP.NET webform?

Although ASP.NET Web API is packaged with ASP.NET MVC, it is easy to add Web API to a traditional ASP.NET Web Forms application. To use Web API in a Web Forms application, there are two main steps: Add a Web API controller that derives from the ApiController class. Add a route table to the Application_Start method.


1 Answers

Add the below configuration inside your Web.config file under <system.web> node (assuming your app runs on .NET 4.5, targetFramework attribute is set to 4.5):

<compilation targetFramework="4.5">
  <assemblies>
    <add assembly="System.Net.Http, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
  </assemblies>
</compilation>

Also add the below one at the root level under <configuration> node:

<runtime>
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <dependentAssembly>
      <assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" />
      <bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
    </dependentAssembly>
  </assemblyBinding>
</runtime>

This should solve your problem.

like image 89
tugberk Avatar answered Sep 22 '22 03:09

tugberk