ASP.net Web API和System.Net.Http

use*_*945 9 asp.net-mvc asp.net-web-api

按照本文中的示例:http://blogs.msdn.com/b/yaohuang1/archive/2012/05/21/asp-net-web-api-generating-a-web-api-help-page-using -apiexplorer.aspx

我已经设置了所有内容来为我的Web API项目提供文档,但我遇到了一个问题.当我尝试使用时,@api.HttpMethod我得到了他在文章中途描述的错误.他说你必须System.Net.Http在web.config中手动添加对Version = 2.0.0.0程序集的引用(即使它默认位于References文件夹中),但是如果你按照他的例子添加程序集的传统方式通过web.config中的标签.....你会发现它不再是4.5中的有效标签,一切都是通过AssemblyRedirects完成的.我尝试了但无济于事.任何人有这个问题或知道如何帮助改变web.config?我错过了一次会议吗?

Visual Studio 2012 MVC4 Web API项目(不是来自Nuget,VS2012附带的最终版本)

tug*_*erk 26

<system.web>节点下的Web.config文件中添加以下配置(假设您的应用程序在.NET 4.5上运行,targetFramework属性设置为4.5):

<compilation targetFramework="4.5">
  <assemblies>
    <add assembly="System.Net.Http, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
  </assemblies>
</compilation>
Run Code Online (Sandbox Code Playgroud)

还要在<configuration>节点下的根级别添加以下内容:

<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>
Run Code Online (Sandbox Code Playgroud)

这应该可以解决您的问题.

  • 为什么这有必要? (4认同)
  • 我一直在与这个愚蠢的问题作斗争,以上解决了它......非常感谢 (2认同)
  • 我在这个错误上花了一整天,只有这篇文章修复了它。嗯嗯! (2认同)