Razor 2到Razor 3 MVC 5

Dyl*_*eau 54 c# asp.net-mvc razor asp.net-mvc-4 asp.net-mvc-5

我一直在研究MVC 4解决方案,我一直在尝试将其升级到MVC 5.我已按照此处列出的步骤进行操作.

我已经关注它,现在每当我运行MVC应用程序时,它都会给我这个错误消息:

[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:\Users\User\AppData\Local\Temp\Temporary ASP.NET 
Files\root\665ac028\de53a189\assembly\dl3\c2c0a4b5\56e8099e_40e0ce01\System.Web.WebPages.Razor.dll'.
Run Code Online (Sandbox Code Playgroud)

有谁知道这可能是怎么产生的?或者如何解决?到目前为止我环顾四周?我试过更改web.config文件,但没有用...

Dim*_*ima 107

在你Web.config(s)表示,确保assemblyBinding包含了装配正确的版本System.Web.WebPages.RazorSystem.Web.Mvc.

<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
  <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>
</assemblyBinding>
Run Code Online (Sandbox Code Playgroud)

并确保剃刀sectionGroupConfigSections参考最新版本以及:

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

  • @Dima我将我的项目从Framework 4.0更改为4.5.1并安装MVC 5.2.3并面临同样的问题.我按照你的回答发现主web.config中只缺少以下内容,<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> (2认同)