Using Razor V2 in ASP.NET MVC 3

        Introduction:


                    Razor view engine's popularity and usage is continuously increasing because it is very easy to learn and write. Also, it is very clean and easy to maintain. The beauty of Razor is that you can host it in any application not just in ASP.NET MVC or ASP.NET Web Pages. The latest version of Razor is 2 which is released with ASP.NET MVC 4 and ASP.NET Web Pages 2. Razor V2 includes a bunch of new features. With the cool new features of Razor, you may want to use these features in your ASP.NET MVC 3 application. In this article, I will show you how you can use Razor V2 in ASP.NET MVC 3 application.


        Description:


                    For testing purpose, you can create a new ASP.NET MVC 3(Razor) application. From Solution Explorer, remove the reference of System.Web.WebPages 1.0 and System.Web.Helpers 1.0. Then, add the reference of System.Web.WebPages 2.0 and System.Web.Helpers 2.0.


                     


                    Inside root web.config file, add/update these settings,  


      <appSettings>
            <add key="webpages:Version" value="2.0.0.0"/>
            <add key="webpages:Enabled" value="true" />
            <add key="PreserveLoginUrl" value="true" />
.............................................................................................................................
.............................................................................................................................
.............................................................................................................................
      <add assembly="System.Web.WebPages, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
      <add assembly="System.Web.Helpers, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
.............................................................................................................................
.............................................................................................................................
.............................................................................................................................
      <dependentAssembly>
            <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
            <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
             <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
	    <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
      </dependentAssembly>
.............................................................................................................................
.............................................................................................................................
.............................................................................................................................

                    Inside Views web.config file, add/update these settings,


      <configSections>
		<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>
      </configSections>
      <system.web.webPages.razor>
		<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />

                    Now you can easily use Razor V2 features in inside ASP.NET MVC 3 view.


      @{
            int? nullValue = null;
      }

      <a href="~/WebForm1.aspx" class="@nullValue">MyLink</a>


                    Note: Before doing this, make sure you have installed ASP.NET MVC 4. Otherwise the trick will fail.


        Summary:


                    There is a bunch of improvement and new features in Razor V2. In this article, I showed you how you can add Razor V2 inside ASP.NET MVC 3 application. I have also attached a sample ASP.NET MVC 3 application with above changes. Hopefully you will enjoy this article too.



No Comments