2012-10-12 37 views
8

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

我已經設置了一切爲我的Web API項目提供文檔,但我遇到的一個問題。當我嘗試使用@api.HttpMethod時,我收到了他在文章中途描述的錯誤。他說你必須在web.config中手動添加對System.Net.Http Version = 2.0.0.0程序集的引用(即使默認情況下它位於References文件夾中),但是如果按照他的傳統方式添加程序集的示例在web.config中的標籤.....好吧,你會發現這不再是4.5中的有效標籤,一切都通過AssemblyRedirects完成。我嘗試過,但無濟於事。任何人有這個問題或知道如何幫助改變web.config?我錯過了一次會議嗎?

的Visual Studio 2012 MVC4的Web API項目(不從的NuGet,隨VS2012最終發佈)

+0

我無法在VS 2012 MVC4 Web API項目上重現您的問題。在將System.Net.Http的引用添加到web.config後,你會得到什麼異常? –

回答

22

添加您的Web.config文件中下面的配置下<system.web>節點(假設你的應用程序在.NET 4.5中運行,targetFramework屬性被設置爲4.5):

<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> 

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

<configuration>節點還要添加下面一個在根級別

這應該可以解決您的問題。

+4

這是爲什麼這是必要的? – Adam

+4

沒有人知道... – GreyCloud

+1

我一直在爭取這個愚蠢的問題永遠和上述固定它......非常感謝 – 99823