2013-07-30 96 views
112

我有這樣一個問題,在服務器上網站停止了在asp.net System.Web.WebPages.Razor.Configuration.HostSection工作不能轉換到

[A]System.Web.WebPages.Razor.Configuration.HostSection cannot be cast to [B]System.Web.WebPages.Razor.Configuration.HostSection. Type A originates from 'System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' in the context 'Default' at location 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Web.WebPages.Razor\v4.0_2.0.0.0__31bf3856ad364e35\System.Web.WebPages.Razor.dll'. Type B originates from 'System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' in the context 'Default' at location 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Web.WebPages.Razor\v4.0_1.0.0.0__31bf3856ad364e35\System.Web.WebPages.Razor.dll'.

說明:執行過程中發生未處理的異常當前的Web請求。請查看堆棧跟蹤以獲取有關該錯誤的更多信息以及源代碼的位置。

Exception Details: System.InvalidCastException: [A]System.Web.WebPages.Razor.Configuration.HostSection cannot be cast to [B]System.Web.WebPages.Razor.Configuration.HostSection. Type A originates from 'System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' in the context 'Default' at location 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Web.WebPages.Razor\v4.0_2.0.0.0__31bf3856ad364e35\System.Web.WebPages.Razor.dll'. Type B originates from 'System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' in the context 'Default' at location 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Web.WebPages.Razor\v4.0_1.0.0.0__31bf3856ad364e35\System.Web.WebPages.Razor.dll'.

源錯誤:

我已經在塞雷爾語和我的電腦上安裝了MVC 3。我試圖用15天的代碼庫替換它,但同樣的問題存在。

現在我想解決它。做一個人知道我如何解決它。

+0

好像在應用程序中的一些組件有不同版本的剃刀的依賴關係。 嘗試使用ildasm或.NET Reflector檢查您的程序集以查看其依賴關係。 –

回答

0

我發現了一個問題和解決方案。

1我將我的代碼定位到4.0,然後上傳代碼並運行。這之前,我也更新我的代碼ASP.NET MVC 4

現在它的工作:)

+1

您是如何更新代碼的? – RetroCoder

+3

你能更具體嗎? – nwayve

199

根Web.config確保assemblyBinding包含的組件「System.Web.WebPages正確的版本。剃刀「和」System.Web.Mvc「。檢查他們的實際存在以及我的「System.Web.WebPages.Razor」程序集標記丟失導致錯誤。在撰寫本文時,我的程序集版本分別設置爲3.0和5.0,並提供完整的NuGet更新。代碼應該如下所示。 publicKeyToken在版本之間保持不變。乾杯!

<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> 
    <!--some more bidings--> 
    <dependentAssembly> 
     <assemblyIdentity name="System.Web.WebPages.Razor" 
      publicKeyToken="31bf3856ad364e35" /> 
     <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0"/> 
    </dependentAssembly> 

    <dependentAssembly> 
    <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" /> 
    <bindingRedirect oldVersion="1.0.0.0-5.0.0.0" newVersion="5.0.0.0" /> 
    </dependentAssembly> 
<!--some more bidings--> 
</assemblyBinding> 
+1

這對我有效,謝謝你的更詳細的迴應。 – nwayve

+2

爲我工作。我錯過了Razor信息,MVC指出了錯誤的版本。 – Hoakie

+0

與@Hoakie相同的情況 - 將Razor更新爲3.0.0,將MVC更新爲5.0.0,並且沒有Razor dependentAssembly引用,並且MVC仍然表示oldVersion =「1.0.0.0-4.0.0.0」newVersion =「4.0.0.0」。在上面添加了Razor dependentAssembly XML,並將4.0.0.0更改爲5.0.0.0,並將Bob的叔叔更改爲5.0.0.0。 –

34

萬一伊恩的回答是不夠的(和組件正確的,但鑄件仍然是錯誤的)機會是你沒有更新<configSections>以反映新的組件呢。確保程序集部分也引用了System.Web.WebPages.Razor庫的版本3.0。例如

<sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"> 
    <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" /> 
    <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" /> 
</sectionGroup> 

(請注意,他們都引用version=3.0.0現在)

+8

我只是補充說,如果您在根Web.Config中找不到這些設置,請檢查Web視圖文件夾中的Web.Config。 – gligoran

+4

是的問題是我的視圖文件夾web.config。 –

15

伊恩的解決方案在一個項目的工作,但對於其他人,我需要在How to Upgrade to MVC 5 and Web API 2

+0

這篇文章爲我做了。謝謝! – gligoran

+0

我認爲這篇文章包含了最正確和最完整的升級說明,謝謝。 –

1

的更完整的信息這個問題是很常見的,當你爲MVC 4開發,然後突然你安裝一個在新版本中可用的軟件包,因此它打破了整個應用程序。

只有解決了這樣的問題是整個應用程序升級到較新的或安裝兼容老包與應用程序

39

我的情況下,從mvc4升級到mvc5。

我確保我的引用與新創建的mvc5應用程序同步,並相應地更新了web.config文件,但仍然無法正常工作。 它終於工作,當我改變我的網站。從配置文件(內views目錄):

<sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"> 
    <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" /> 
    <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" /> 
</sectionGroup> 

要這樣:

<sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"> 
    <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" /> 
    <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" /> 
</sectionGroup> 

記住:停止IIS(明示或本地),乾淨的解決方案,建立和運行。

+0

我認爲這個回答實際上應該是一個答案。使用update-package方法從mvc4升級到mvc5時,程序集綁定通常是可以的。 – Roman

1

問題:

[A]System.Web.WebPages.Razor.Configuration.HostSection cannot be cast to [B]System.Web.WebPages.Razor.Configuration.HostSection. Type A originates from 'System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' in the context 'Default' at location 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Web.WebPages.Razor\v4.0_2.0.0.0__31bf3856ad364e35\System.Web.WebPages.Razor.dll'. Type B originates from 'System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' in the context 'Default' at location 'C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\rbs.ceds.todomvcdemo\73d0b7c7\46e3986d\assembly\dl3\21020800\101279fc_da94cf01\System.Web.WebPages.Razor.dll'.

鑑定:

錯誤與項目的配置文件,即web.config中。

在這裏您會發現Razor版本設置爲2.0.0.0,因爲您的項目2.0.0.0版本不可用。

解決方案:

它是簡單的,只是改變從2.0.0.0版本到3.0.0.0,它會做的魔力。

type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0 type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.0.0.0

你不需要重定向,所以它會使事情變得複雜。

0

打開系統中的兩個dll文件位置將文件位置中的一個dll複製到另一個
並嘗試運行該項目。

0

另一種方法來解決這個問題,而無需更新引用這是

<runtime> 
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> 
     <dependentAssembly> 
     <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" /> 
     <bindingRedirect oldVersion="1.0.0.0-5.0.0.0" newVersion="3.0.0.0" /> 
     </dependentAssembly> 
     <dependentAssembly> 
     <assemblyIdentity name="System.Web.Pages.Razor" publicKeyToken="31bf3856ad364e35" culture="neutral" /> 
     <bindingRedirect oldVersion="0.0.0.0-3.2.0.0" newVersion="1.0.0.0" /> 
     </dependentAssembly> 
    </assemblyBinding> 
    </runtime> 
相關問題