asp.net-mvc 站点在 asp.net 中停止工作 System.Web.WebPages.Razor.Configuration.HostSection 无法转换为
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17941053/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Site stopped working in asp.net System.Web.WebPages.Razor.Configuration.HostSection cannot be cast to
提问by user2632851
I have a problem like this on server
我在服务器上有这样的问题
[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'.
[A]System.Web.WebPages.Razor.Configuration.HostSection 无法转换为 [B]System.Web.WebPages.Razor.Configuration.HostSection。类型 A 源自“C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System”上下文“默认”中的“System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35” .Web.WebPages.Razor\v4.0_2.0.0.0__31bf3856ad364e35\System.Web.WebPages.Razor.dll'。类型 B 源自“C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System”上下文“默认”中的“System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35” .Web.WebPages.Razor\v4.0_1.0.0.0__31bf3856ad364e35\System.Web.WebPages.Razor.dll'。
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
说明:在执行当前 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'.
异常详细信息:System.InvalidCastException:[A]System.Web.WebPages.Razor.Configuration.HostSection 无法转换为 [B]System.Web.WebPages.Razor.Configuration.HostSection。类型 A 源自“C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System”上下文“默认”中的“System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35” .Web.WebPages.Razor\v4.0_2.0.0.0__31bf3856ad364e35\System.Web.WebPages.Razor.dll'。类型 B 源自“C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System”上下文“默认”中的“System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35” .Web.WebPages.Razor\v4.0_1.0.0.0__31bf3856ad364e35\System.Web.WebPages.Razor.dll'。
Source Error:
源错误:
I have installed MVC 3 on serer and on my computer. I have tried with replacing it with 15 days old codebase but the same problem exist.
我已经在服务器和我的电脑上安装了 MVC 3。我尝试用 15 天旧的代码库替换它,但存在同样的问题。
now I want to solve it. Do someone know how I can fix it.
现在我想解决它。有人知道我该如何修复它。
回答by Ian Flynn
In the root Web.config make sure assemblyBinding contains the proper version for the assembly "System.Web.WebPages.Razor" and "System.Web.Mvc". Check for their actual existence as well as my "System.Web.WebPages.Razor" assembly tag was missing causing the error. I had my assembly versions set to 3.0 and 5.0 respectively in the time of this writing with full NuGet updates. Code should look something like below. The publicKeyToken will stay the same between the versions. Cheers!
在根 Web.config 中,确保 assemblyBinding 包含程序集“System.Web.WebPages.Razor”和“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>
回答by user3141889
My case was upgrading from mvc4 to mvc5.
我的情况是从 mvc4 升级到 mvc5。
I made sure my references were in sync (with a newly created mvc5 app), updated the web.config file accordingly and it still didn't work. It finally worked when i changed my web.config file (INSIDE THE VIEWS directory) from:
我确保我的引用是同步的(与新创建的 mvc5 应用程序),相应地更新了 web.config 文件,但它仍然不起作用。当我从以下位置更改我的 web.config 文件(INSIDE THE 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>
To this:
对此:
<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>
REMEMBER: stop iis (express or native), clean solution, build and Run.
记住:停止 iis(express 或 native),清理解决方案,构建并运行。
回答by Brad Christie
Just in case ian's answer wasn't enough (and the assemblies correct, but the casting is still wrong) chances are you didn't update the <configSections>to reflect the new assembly yet. make sure the assembly portion also references version 3.0 of the System.Web.WebPages.Razorlibrary. e.g.
以防万一 ian 的回答还不够(并且程序集是正确的,但演员表仍然是错误的)您可能还没有更新<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>
(Notice that they all reference version=3.0.0now)
(注意他们version=3.0.0现在都引用了)
回答by Ken Mc
Ian's solution worked in one project, but for others I need the more complete info in How to Upgrade to MVC 5 and Web API 2
Ian 的解决方案适用于一个项目,但对于其他项目,我需要如何升级到 MVC 5 和 Web API 2 中的更完整信息
回答by ProgrammingNinja
This issue is very common when you are developing for MVC 4 and then suddently you install a package which is available in newer version so it breaks entire application.
当您为 MVC 4 进行开发时,这个问题很常见,然后您突然安装了一个在较新版本中可用的包,因此它会破坏整个应用程序。
Only solution to such issue is to upgrade your entire application to newer or install the old package compatible with your application
解决此类问题的唯一方法是将您的整个应用程序升级到更新版本或安装与您的应用程序兼容的旧软件包
回答by codebased
Problem:
问题:
[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'.
[A]System.Web.WebPages.Razor.Configuration.HostSection 无法转换为 [B]System.Web.WebPages.Razor.Configuration.HostSection。类型 A 源自“C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System”上下文“默认”中的“System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35” .Web.WebPages.Razor\v4.0_2.0.0.0__31bf3856ad364e35\System.Web.WebPages.Razor.dll'。类型 B 源自“C:\Windows\Microsoft.NET\Framework64\v4.0.30319”上下文“默认”中的“System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35” \Temporary ASP.NET Files\rbs.ceds.todomvcdemo\73d0b7c7\46e3986d\assembly\dl3\21020800\101279fc_da94cf01\System.Web.WebPages.Razor.dll'。
Identification:
鉴别:
The error is with the configuration file i.e. web.config of your project.
错误与您的项目的配置文件即 web.config 有关。
Here you will find that Razor version is set to 2.0.0.0 where as in your project 2.0.0.0 version is not available.
在这里,您会发现 Razor 版本设置为 2.0.0.0,而在您的项目中,2.0.0.0 版本不可用。
Solution:
解决方案:
It is simple, just change the version from 2.0.0.0 to 3.0.0.0 and it will do the magic.
很简单,只需将版本从 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
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 , 版本=3.0.0.0
You don't need to put redirection or so as it will make things complicated.
您不需要放置重定向等,因为它会使事情变得复杂。
回答by user4287068
Open the two dll files location in your system copy one of the dll in your file location to the another
and try to run the project.
打开系统中的两个 dll 文件位置,将文件位置中的一个 dll 复制到另一个,
然后尝试运行该项目。
回答by Fernando Gonzalez Sanchez
Another way to fix this, without having to update references is this
无需更新引用即可解决此问题的另一种方法是
<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>
回答by user2632851
I found a problem and solution.
我发现了一个问题和解决方案。
1 I target my code to 4.0 and then upload the code and it's worked. Before it I also update my code to asp.net MVC 4.
1 我将我的代码定位到 4.0,然后上传代码并且它起作用了。在此之前,我还将我的代码更新为 asp.net MVC 4。
now it's work :)
现在它的工作:)

