MVC error: “System.Web.WebPages. Razor.Configuration. HostSection cannot be cast…”

Got this error when doing a partial deployment to an MVC ASP.Net website. This one took me a while to figure out as the some of the changes in the code was done by someone working with an earlier version of Visual Studio (while I was working with 2015)..

[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’.

Took a while to find out the root cause. Apparently this is due to incorrect versions specified in the web.config in the VIEWS folder (not just the root folder).

Simply replace the configsection in there to reflect the new version:

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

Hope that helps!

Leave a Comment

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

*
*