An InvalidCastException is thrown after installing a beta version of ASP.NET MVC 4

I get the following exception after installing ASP.NET MVC 4 Beta on a machine with ASP.NET MVC 3.

Correction System.InvalidCastException was not processed by user code Message = [A] System.Web.WebPages.Razor.Configuration.HostSection could not be added 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 of" Default "in the 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 is taken from "System.Web.WebPages.Razor, Version = 2.0.0.0, Culture = neutral, PublicKeyToken = 31bf3856ad364e35" in the "Default" context in the location "C: \ Windows \ Microsoft.Net \ assembly \ GAC_MSIL \ System .Web.WebPages.Razor \ v4.0_2.0.0.03131bf3856ad364e35 \ System.Web.WebPages.Razor.dll '. Source = System.Web.WebPages.Razor

Stacktrace:

in System.Web.WebPages.Razor.WebRazorHostFactory.GetRazorSection (String virtualPath) in System.Web.WebPages.Razor.WebRazorHostFactory.CreateHostFromConfig (String virtualPath, String physicalPath) in System.Web.WebGazPor.FebRazRobRazRazRobRaz.PromPaz.PebRazRobRaz.PromPaz.PebRazRobRaz.PebRazRobRaz.PromPaz.PebRazRobRaz.PromPaz.FebRaz.PebRazRobRaz.PebRazRobRaz.PromPaz.FebRazRobRaz.PromPaz.FebRazRobRaz.PromPaz.FebRaz.PromPost. System.Web.WebPages.Razor.RazorBuildProvider.CreateHost () in System.Web.WebPages.Razor.RazorBuildProvider.EnsureGeneratedCode () in System.Web.WebPages.Razor.RazorBuildProvider.get_WodeCompiler.ildpodepiler.ildpodepiler.ildpodepiler.ilerpild. GetCompilerTypeFromBuildProvider (BuildProvider buildProvider) in System.Web.Compilation.BuildProvidersCompiler.ProcessBuildProviders () in System.Web.Compilation.BuildProvidersCompiler.PerformBuild () in System.Web.Compilation.BuilePathFeb.PhilePeb.FileWebPathFile.eb BuildManager.GetVPathBuildResultInternal (VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowB uildInPrecompile, Boolean throwIfNotFound, Boolean obespechivaetIsUpToDate) in System.Web.Compilation.BuildManager.GetVPathBuildResultWithNoAssert (HttpContext context, VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile, Boolean throwIfNotFound, Boolean obespechivayutIsUpToDate) in System.Web.Compilation.BuildManager.GetVirtualPathObjectFactory (VirtualPath virtualPath, HttpContext context, Boolean allowCrossApp, Boolean throwIfNotFound) in System.Web.Mvc.BuildManagerWrapper.System.Web.Mvc.IBuildManager.FileExists (String virtualPath), System.Web.Mvc.BuildConFileController Controller virtualPath) in System.Web.Mvc.VirtualPathProviderViewEngine.GetPathFromGeneralName (ControllerContext controllerContext, List`1 location, String name, String controllerName, String areaName, String cacheKey, String [] & lookLocations) in Sy stem.Web.Mvc.VirtualPathProviderViewEngine.GetPath (ControllerContext controllerContext, String [] location, String [] areaLocations, String locationsPropertyName, String name, String controllerName, String cacheKeyPrefix, Boolean useCache, String [] & lookLocations) in System.Web.v .VirtualPathProviderViewEngine.FindView (ControllerContext controllerContext, String viewName, String masterName, Boolean useCache) in System.Web.Mvc.ViewEngineCollection. <> c__DisplayClassc.b__b (IViewEngine e) in System.Web.Mvc.ViewEngineCollection.Find (Func`2 lookup, Boolean trackSearchedPaths) in System.Web.Mvc.ViewEngineCollection.FindView (ControllerContext controllerContext, String viewName, String viewName, String masterName, String masterName .Web.Mvc.ViewResult.FindView (ControllerContext context) in System.Web.Mvc.ViewResultBase.ExecuteResult (ControllerContext context) in System.Web.Mvc.ControllerActionInvoker.InvokeActionResult (ControllerContext controllerContext, ActionResult). ControllerActionInvoker. <> c__DisplayClass1c.b__19 () in System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter (IResultFilter filter, ResultExecutingContext preContext, continued Func`1) in System.Web.Mvc.ControllerActionInvoker. <> c__DisplayClass1c. <> c__DisplayClass1e.b__1b () in System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters (ControllerContext controllerContext, IList`1, ActionResult actionResult) in System.Web.Mvc.ControllerActionInvoker.InvokeActionConverter Controller (Action) Mvc.Controller.ExecuteCore () in [project_namespace] .Controllers.GlobalizationController.ExecuteCore () in [file_path] in System.Web.Mvc.ControllerBase.Execute (RequestContext requestContext) in System.Web.Mvc.ControllerBase.System.Web. Mvc.IController.Execute (RequestContext requestContext) in System.Web.Mvc.MvcHandler. <> c__DisplayClass6. <> c__DisplayClassb.b__5 () in System.Web.Mvc.Async.AsyncResultWrapper. <> c__DisplayClass1.b__0 () in System.Web.Mvc.Async.AsyncResultWrapper. <> c__DisplayClass8`1.b__7 (IAsyncResult _) in System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End () in System.Web.Mvc.MvcHandler. <> c__DisplayClasse.b__d () in System.Web.Mvc.SecurityUtil.b__0 (Action f) in System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust (action in action) in System.Web.Mvc.MvcHandler.EndProcessRequest (IAsyncResult) System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest (result of IAsyncResult) in System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Executepep.ep () logical and complete synchronously)

This completely blocks me, and although I would like to keep the beta version, I may need to completely remove it to get rid of the exception.

+80
asp.net-mvc razor asp.net-mvc-4 asp.net-webpages
Feb 22 2018-12-22T00:
source share
9 answers

Try adding the following to your web.config:

<appSettings> <add key="webpages:Version" value="1.0.0.0"/> </appSettings> 

The problem may be due to the fact that several registered suppliers of the assembly and the last win.

+32
Feb 22 '12 at 22:18
source share

I had the same problem, but when porting ASP.NET MVC 3 to ASP.NET MVC 4. So I got this thread. I found a solution to the problem, but its source is different from the WebPages version.

I followed the official guide (so to speak) Upgrading an ASP.NET MVC 3 project to ASP.NET MVC 4 .

To fix the problem, you need to add to Web.Config (possibly previously added):

 <runtime><!-- Should be there by default, near end --> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"><!-- Should be there by default --> <dependentAssembly>..</dependentAssembly><!-- Should be there by default --> ... some other dependecy redirects ... <dependentAssembly> <assemblyIdentity name="System.Web.WebPages.Razor" publicKeyToken="31bf3856ad364e35" /> <bindingRedirect oldVersion="1.0.0.0" newVersion="2.0.0.0"/> </dependentAssembly> ... some other dependency redirects ... </assemblyBinding> </runtime> 

Also, the notes do not mention changing the System.Web.Helpers link from 1.0.0.0 to 2.0.0.0 (delete and add a new one).

+100
May 11 '12 at 18:37
source share

Another, rather late solution ... I ran into this problem. All the fixes listed here (Thanks everyone!), But could not get past the error. Finally, in desperation, I found the web.config file in the views directory. In this file I found several links to the version of System.Web.Mvc and typed all of them up to "4.0.0.0". More importantly, I found the following lines and updated the version numbers for "system.web.webPages.Razor" from "1.0.0.0" to "2.0.0.0", and suddenly everything worked again. Hope this helps someone else.

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

Steve G.

+34
Jan 10 '13 at 23:16
source share

I found another way to get this to work on a Taiwanese blog when upgrading from MVC 2 to MVC 3: http://blog.kkbruce.net/2011_04_01_archive.html (In fact, you can use Microsoft Translate in IE10 to see the English version, and this pretty good translation ...)

The trick is to find all versions related to MVC3 (such as Razor, Mvc, Helper builds, you will know what they are if you go to your links and view all new versions of your link build) and change them from 1.0.0.0 to 2.0.0.0 and from 3.0.0.0 to 4.0.0.0 (since the public keys are the same). You can also try to remove versions according to the block.

Another important thing - these settings are found in all Web.Configs. Therefore, not only as root, but also as View, Area / {Area} / View, etc.

I feel that this solution may allow you to completely switch to MVC4 instead of having a hybrid that didn't work for me anyway.

+5
Mar 05 2018-12-12T00:
source share
  • In Visual Studio, create a new, one-time ASP.NET MVC 4 application in a separate folder.
  • Replace the web.config file in the Views folder with the freshly Views file. web.config from the new throwaway application.

You will notice:

System.Web.WebPages.Razor changes from Version=1.0.0.0 to Version=2.0.0.0 System.Web.Mvc changes from Version=3.0.0.0 to Version=4.0.0.0

+4
May 24 '13 at 03:19
source share

If you run into this problem and you tried the solutions above and it didn’t work, try checking web.config in the Views folder. There may be some configurations that conflict with the Web.config file in the root folder of the application

+3
Jan 21 '13 at 14:59
source share

Some of your codes work on current MVC bits, while others work on newer ones. Without understanding the code, I cannot tell you which specific bit is causing the problem.

Having said that, I would never have created a beta on a product development machine. It's too easy to create a virtual machine these days and run the beta on the image. On Windows 7+, you can download an image from metal and not take performance if you need it (using the dev tool betas, you often do it).

0
Feb 22 '12 at 22:16
source share

For completeness ...

You can also completely disable web pages if you are not using them.

 <appSettings> <add key="webpages:Enabled" value="false" /> </appSettings> 

(I still don't quite understand the connection between razor views and web pages, but it seems my razor-view MVC application still works with disabled web pages.)

0
Jun 28 '12 at 16:08
source share

Also, as a last step, clear the bin folder from the project from which the error is coming.

0
May 31 '13 at 13:38
source share



All Articles