Site stopped working in asp.net System.Web.WebPages.Razor.Configuration.HostSection cannot be cast to

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

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.

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

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.

now I want to solve it. Do someone know how I can fix it.

47884 次浏览

我找到了一个问题和解决方案。

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.

现在是工作:)

在根 Web.config 中,确保 AssemblyBinding 包含程序集“ System”的正确版本。韦伯。网页。“剃刀”和“系统”。韦伯。MVC.检查他们的实际存在,以及我的“系统。韦伯。网页。“ 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>

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.Razor library. e.g.

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

伊恩的解决方案在一个项目中工作,但对于其他人,我需要在 如何升级到 MVC5和 WebAPI 2更完整的信息

这个问题在你为 MVC 4开发的时候很常见,然后你突然安装了一个新版本的软件包,所以它破坏了整个应用程序。

解决这个问题的唯一方法是将整个应用程序升级到更新的版本,或者安装与应用程序兼容的旧包

我的案子是从 mvc4升级到 mvc5。

我确保我的引用是同步的(与一个新创建的 mvc5应用程序同步) ,相应地更新了 web.config 文件,但它仍然不能工作。 当我将 web.config 文件(视图目录内部)从:

<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 (表达式或本机) ,干净的解决方案,构建和运行。

问题:

[ A ]系统。网页。网页。剃刀。配置。主机节不能被强制转换 到[ B ]系统。网页。网页。剃刀。配置。主机节。类型 A 源自“ System.Web.WebPages. Razor,Version = 2.0.0.0, 区域性 = 中性,PublicKeyToken = 31bf3856ad364e35’ “默认”位置 ‘ C: Windows Microsoft. Net 程序集 GAC _ MSIL System. Web. WebPages. Razor v4.0 _ 2.0.0.0 _ _ 31bf3856ad364e35 System. Web. WebPages. Razor.dll’。 类型 B 源自‘ System.Web.WebPages. Razor,Version = 3.0.0.0, 区域性 = 中性,PublicKeyToken = 31bf3856ad364e35’ “默认”位置 C: Windows Microsoft.NET Framework64 v4.0.30319临时 ASP.NET 文件 rbs.ceds.todomvcdemo 73d0b7c746e3986d 汇编 dl321020800101279fc _ da94cf01 System. WebPages. Razor.dll’。

Identification:

错误出现在项目的配置文件中,即 web.config。

在这里,您会发现 Razor 版本被设置为2.0.0.0,而您的项目2.0.0.0版本不可用。

解决方案:

It is simple, just change the version from 2.0.0.0 to 3.0.0.0 and it will do the magic.

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

您不需要添加重定向,否则会使事情变得复杂。

打开系统中的两个 dll 文件位置,将文件位置中的一个 dll 复制到另一个
并尝试运行该项目。

解决这个问题的另一种方法是,不必更新引用

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