Updating to ASP.NET MVC 5 via NuGet

It appears the NuGet package for MVC 5 causes the web application to throw errors after updating.

After updating, running the web application in debug mode causes the following error to be displayed:

[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:\Users\xxx\AppData\Local\Temp\Temporary  ASP.NET  Files\root\ed46e624\f978c369\assembly\dl3\6c3a6840\098297a3_ebcfce01\System.Web.WebPages.Razor.dll’.

To fix this issue, you must edit the Web.Config file under the Views folder (not the primary Web.Config).  Replace each section with the following sections:

Under the ‘ConfigSections’ key:

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

Under the ‘system.web.webPages.razor’ key:

<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />

Under the ‘system.web’ key:

<pages
validateRequest="false"
pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<controls>
<add assembly="System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" namespace="System.Web.Mvc" tagPrefix="mvc" />
</controls>
</pages>

Save the file and now the application should run.

You may also like...

4 Responses

  1. Marius says:

    But intellisense not working :(

  2. Yes. I continued to have weird issues using Visual Studio 2012. Once I downloaded and installed Visual Studio 2013 (now that it has been released), all my problems went away.

  3. Sean says:

    Thanks for this – helped me to finally get Intellisense working, the big problem seems to be that the versions have to be manually changed in the web.config in the Views folder every time a new update of MVC comes out. I actually had to use 5.1.0.0 where you are showing 5.0.0.0 in this post.

  4. Jeremy says:

    Excellent article. I’m dealing with many of these issues as well..

Leave a Reply

Your email address will not be published. Required fields are marked *