What is this error in ASP.NET MVC 3 RTM Project?

2020-07-02 08:43发布

问题:

I Create new Project ASP.NET MVC 3 RTM and Replace References for using ASP.NET MVC 3 Source code

but When I run project again, get this exception:

[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=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'. Type B originates from 'System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' in the context 'Default' at location 'C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\2505c7ee\369315c5\assembly\dl3\f7bae428\d03eeed8_85bbcb01\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.

Source Error:

Line 106:        internal static RazorWebSectionGroup GetRazorSection(string virtualPath) {
Line 107:            // Get the individual sections (we can only use GetSection in medium trust) and then reconstruct the section group
Line 108:            return new RazorWebSectionGroup() {
Line 109:                Host = (HostSection)WebConfigurationManager.GetSection(HostSection.SectionName, virtualPath),
Line 110:                Pages = (RazorPagesSection)WebConfigurationManager.GetSection(RazorPagesSection.SectionName, virtualPath)

Source File: E:\GreenTour\webpages\src\System.Web.WebPages.Razor\WebRazorHostFactory.cs Line: 108

回答1:

This error means that part of your application is being compiled using your own copy of the source code as a reference while at runtime its finding the reference assemblies in the GAC. Make sure you remove all references to the GACed version of MVC 3 assemblies. This includes a number of things in your web.config files that use this public key token: 31bf3856ad364e35.



回答2:

I just came across the same problem whilst upgrading my app, the problem was in the web.config file located in the views directory, I blindly copied this over from the old app (my bad).

My solution , create a new MVC 4 app and copy the web.config from the views directory to your app thats having the problem.

Hope this helps



回答3:

Although the above thread was useful. I could not get things working until I created a 'dummy' 'basic' MVC 4 application and then compared the web.configs to find any changes.



回答4:

I fixed this not by removing all references to assemblies with the public key of 31bf3856ad364e35 as this covers a lot of irrelevant assemblies.

Instead it should be fixed by updating all configuration references

  • System.Web.Mvc to 4
  • System.Web.WebPages to 2
  • System.Web.WebPages.Razor to 2

Be sure to update all the web.configs, including the ones in View directories.