Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Razor 2 to Razor 3 MVC 5

I've been working on an MVC 4 solution, and I've been trying to upgrade it to MVC 5. I've followed the steps outlined here.

I've followed it, and now whenever I run the MVC Application, it gives me this error 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=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\User\AppData\Local\Temp\Temporary ASP.NET  Files\root\665ac028\de53a189\assembly\dl3\c2c0a4b5\56e8099e_40e0ce01\System.Web.WebPages.Razor.dll'. 

Does anyone know how this could have originated? or how it can be solved? I've looked around thus far? I've tried changing the web.config files, with no avail...

like image 314
Dylan Corriveau Avatar asked Nov 13 '13 09:11

Dylan Corriveau


People also ask

What is Razor syntax MVC 5?

Razor is a markup syntax that lets you embed server-based code into web pages using C# and VB.Net. It is not a programming language. It is a server side markup language. Razor has no ties to ASP.NET MVC because Razor is a general-purpose templating engine.

Does MVC use Razor?

Razor is one of the view engines supported in ASP.NET MVC. Razor allows you to write a mix of HTML and server-side code using C# or Visual Basic.

What is @page in Razor?

@page says it is Asp.net core Razor pages and not MVC or other pages. The second line has @model. Our case model is IndexModel. Class Name + Model = Model for .cshtml file that's formula. In our case.

What is Cshtml CS file?

cshtml. cs file that has C# code that handles page events.


2 Answers

In your Web.config(-s) make sure assemblyBinding contains the proper version for the assembly System.Web.WebPages.Razor and System.Web.Mvc.

<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">   <dependentAssembly>         <assemblyIdentity name="System.Web.WebPages.Razor" publicKeyToken="31bf3856ad364e35" />         <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>   </dependentAssembly>    <dependentAssembly>     <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />     <bindingRedirect oldVersion="1.0.0.0-5.0.0.0" newVersion="5.0.0.0" />   </dependentAssembly> </assemblyBinding> 

And make sure that razor sectionGroup in ConfigSections reference latest versions as well:

<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> 
like image 142
Dima Avatar answered Sep 23 '22 08:09

Dima


Check for version in web.config. If it again gives an error, try to clean the solution and rebuild it. Also check for project's Bin folder, removes old references from bin folder and rebuild the project solution.

like image 34
sushama Avatar answered Sep 20 '22 08:09

sushama