InvalidCastException is thrown after installing ASP.NET MVC 4 Beta
Asked Answered
H

9

82

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

System.InvalidCastException was unhandled by user code Message=[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=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'. Source=System.Web.WebPages.Razor

StackTrace:

at System.Web.WebPages.Razor.WebRazorHostFactory.GetRazorSection(String virtualPath) at System.Web.WebPages.Razor.WebRazorHostFactory.CreateHostFromConfig(String virtualPath, String physicalPath) at System.Web.WebPages.Razor.RazorBuildProvider.GetHostFromConfig() at System.Web.WebPages.Razor.RazorBuildProvider.CreateHost() at System.Web.WebPages.Razor.RazorBuildProvider.EnsureGeneratedCode() at System.Web.WebPages.Razor.RazorBuildProvider.get_CodeCompilerType() at System.Web.Compilation.BuildProvider.GetCompilerTypeFromBuildProvider(BuildProvider buildProvider) at System.Web.Compilation.BuildProvidersCompiler.ProcessBuildProviders() at System.Web.Compilation.BuildProvidersCompiler.PerformBuild() at System.Web.Compilation.BuildManager.CompileWebFile(VirtualPath virtualPath) at System.Web.Compilation.BuildManager.GetVPathBuildResultInternal(VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile, Boolean throwIfNotFound, Boolean ensureIsUpToDate) at System.Web.Compilation.BuildManager.GetVPathBuildResultWithNoAssert(HttpContext context, VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile, Boolean throwIfNotFound, Boolean ensureIsUpToDate) at System.Web.Compilation.BuildManager.GetVirtualPathObjectFactory(VirtualPath virtualPath, HttpContext context, Boolean allowCrossApp, Boolean throwIfNotFound) at System.Web.Mvc.BuildManagerWrapper.System.Web.Mvc.IBuildManager.FileExists(String virtualPath) at System.Web.Mvc.BuildManagerViewEngine.FileExists(ControllerContext controllerContext, String virtualPath) at System.Web.Mvc.VirtualPathProviderViewEngine.GetPathFromGeneralName(ControllerContext controllerContext, List`1 locations, String name, String controllerName, String areaName, String cacheKey, String[]& searchedLocations) at System.Web.Mvc.VirtualPathProviderViewEngine.GetPath(ControllerContext controllerContext, String[] locations, String[] areaLocations, String locationsPropertyName, String name, String controllerName, String cacheKeyPrefix, Boolean useCache, String[]& searchedLocations) at System.Web.Mvc.VirtualPathProviderViewEngine.FindView(ControllerContext controllerContext, String viewName, String masterName, Boolean useCache) at System.Web.Mvc.ViewEngineCollection.<>c__DisplayClassc.b__b(IViewEngine e) at System.Web.Mvc.ViewEngineCollection.Find(Func`2 lookup, Boolean trackSearchedPaths) at System.Web.Mvc.ViewEngineCollection.FindView(ControllerContext controllerContext, String viewName, String masterName) at System.Web.Mvc.ViewResult.FindView(ControllerContext context) at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) at System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass1c.b__19() at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass1c.<>c__DisplayClass1e.b__1b() at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) at System.Web.Mvc.Controller.ExecuteCore() at [project_namespace].Controllers.GlobalizationController.ExecuteCore() in [file_path] at System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) at System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) at System.Web.Mvc.MvcHandler.<>c__DisplayClass6.<>c__DisplayClassb.b__5() at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass1.b__0() at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass8`1.b__7(IAsyncResult _) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End() at System.Web.Mvc.MvcHandler.<>c__DisplayClasse.b__d() at System.Web.Mvc.SecurityUtil.b__0(Action f) at System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) at System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) at System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

This blocks me completely and while I would like to keep the beta, I might need to remove it completely in order to get rid of the exception.

Habitation answered 22/2, 2012 at 21:38 Comment(6)
Are you running .net 4.5 Dev preview by any chance? (That one is incompatible with the current Beta Version) (just for the record, wasn't me who downvoted, the question is legitimate)Gummous
No, I don't run the 4.5 at all.Habitation
Seriously, why this is down-voted? I provide feedback for something that might happen to others.Habitation
@MichaelStum OK no worries. :)Habitation
Sorry for the confusion. I was trying to "favorite" this question and mis-clicked =P Downvote removed, favorite added correctly this time.Eatmon
@jadarnel27 No problem. :) I try to find out why at run-time the v2.0.0.0 is selected instead of v1.0.0.0 .. :(Habitation
P
32

Try adding the following in your web.config:

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

The issue might be due to multiple build providers being registered and the latest winning.

Picklock answered 22/2, 2012 at 22:18 Comment(0)
E
101

I had the same problem, but when migrating ASP.NET MVC 3 to ASP.NET MVC 4. This way I got on this topic. I have found a solution to the problem, but its source is different, it is not from WebPages version.

I have followed the official guide (so to say) Upgrading an ASP.NET MVC 3 Project to ASP.NET MVC 4.

To resolve the problem, you have to add in Web.Config (probably around previously added ones):

<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 in the notes is not mentioned to change the Reference of System.Web.Helpers from 1.0.0.0 to 2.0.0.0 (delete and add the new one).

Elusive answered 11/5, 2012 at 18:37 Comment(2)
+1 Thanks loads for sharing. One slight variation I made was to use oldVersion="1.0.0.0-2.0.0.0" just to follow the convention used in the MVC4 project template. Interestingly the dependentAssembly for System.Web.WebPages.Razor is also absent from the root Web.config in the MVC4 project template.Claresta
I had a similar problem updating MVC 4 NuGet packages to 5.1.1, resolved with the above but oldVersion="2.0.0.0" newVersion="3.0.0.0"Bonus
T
34

One more, rather late, solution ... I ran into this same problem. Did all the fixes listed here (Thank You All!!!) but could not get past the error. Finally, in desperation, I found a web.config file in the Views directory. In this file, I found several references to the version of System.Web.Mvc and bumped 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 was working once again. Hopefully, this will help 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.

Tilt answered 10/1, 2013 at 23:16 Comment(1)
Also do not forget about web.config under Areas/Views if you're using Areas.Romanaromanas
P
32

Try adding the following in your web.config:

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

The issue might be due to multiple build providers being registered and the latest winning.

Picklock answered 22/2, 2012 at 22:18 Comment(0)
S
5

I found another way of getting this to work, along the line to a Taiwanese's blog on upgrade from MVC 2 to MVC 3: http://blog.kkbruce.net/2011_04_01_archive.html (You can actually use Microsoft Translate in IE10 to see the English version and it is a fairly good translation...)

The trick is to find all the versions that relate to MVC3 (such as Razor, Mvc, Helper assemblies, you will know what they are if you go to your references and check out all the newer versions of your referenced assembly) 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 removing the versions as per the blog.

Another important thing is that these settings are in all the Web.Configs. So not only under you root, but also under View, Area/{Area}/View, etc.

I feel this solution can let you upgrade to the MVC4 completely instead of having a hybrid, which didn't work for me anyways.

Subscript answered 5/3, 2012 at 18:44 Comment(0)
H
4
  1. In Visual Studio, create a new "throwaway" ASP.NET MVC 4 Application in a separate folder somewhere.
  2. Replace the web.config file in your Views folder with the freshly created Views web.config file 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

Hilar answered 24/5, 2013 at 15:19 Comment(0)
U
3

If you are experiencing this issue and you have tried the solutions above and this not work, pls check the web.config in your Views folder. There may be some configurations their that is conflicting the Web.config file in your App root folder

Ungenerous answered 21/1, 2013 at 14:59 Comment(0)
T
0

Some of your code is running on the current MVC bits, while others have been pushed out to the newer. Without understanding the code, I can't tell you what specific bit is causing the issue.

Having said that, I would never set up a beta on a production development machine. It is too easy to create a VM these days and run the beta on an image. In Windows 7+, you can boot the image off the metal and not take the perf hit, if you need that (with dev tool betas, you oft do).

Trochilus answered 22/2, 2012 at 22:16 Comment(3)
Yes, but this Beta has a go-live license. After all, it's on a VM anyways but the VM has also ASP.NET MVC 3.Habitation
You need to determine which bit in 3 is running in 4, as that is where you are having the conflict. The same happened quite a bit in Web Forms as they moved into the .NET 2 timeframe and was quite an issue with any add ons (like MVC?).Trochilus
Actually, it's very big ASP.NET MVC 3 project. Nothing is running in 4. I could not start the 3 project that's all. Adding the <add key="webpages:Version" value="1.0.0.0"/> in appSettings element solved it.Habitation
T
0

For the sake of completeness...

You can also just turn off webpages altogether if you aren't using them.

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

(I'm still not entirely clear on the relationship between razor views and webpages, but it seems my MVC app with razor views still works with webpages disabled.)

Tumbrel answered 28/6, 2012 at 16:8 Comment(0)
D
0

Also as last step clear your bin folder from the project where the error is coming from.

Dihydrostreptomycin answered 31/5, 2013 at 13:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.