Home > ASP.NET MVC > Use Web Form and Razor View Engine in the same project

Use Web Form and Razor View Engine in the same project

September 19, 2011 Leave a comment Go to comments

ASP.Net MVC razor view engine is looking very cool and Microsoft is also focusing on it. But we have already developed a lot with the web form view engine. So we need a way to use both web forms and razor views in a same project or a way to migrate our existing web form pages into razor views side by side.

Below the steps are shown to use both web form pages and razor views in the same application and you can convert your web form pages and partial views side by side.

Step 1: Add reference to if you have the older version of System.Web.Mvc.dll reference.
System.Web.Mvc.dll, v4.0.30319
// C:\Program Files\Microsoft ASP.NET\ASP.NET MVC 3\Assemblies\System.Web.Mvc.dll

Step 2: Add the configuration below in web.config if not available, to use rezor views

<configSections>
<sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
      <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
      <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=1.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=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
      <namespaces>
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Routing" />
        <!-- Your namespace here -->
      </namespaces>
    </pages>
  </system.web.webPages.razor>

Step 3: Create a separate razor layout page equivalent to the web form master page you are using for your site layout. (Create a separate Site.cshtml equivalent to Site.Master).

Step 4: Create razor views (.cshtml) to replace web form pages (.aspx) one by one. We can call web form partial views (.ascx) from razor views so we need not to be worried to replace user controls with razor views. (Replaced Index.aspx with Index.cshtml)

Step 5: Replace web form partial views with razor partial views.

You can see more here http://www.hanselman.com/blog/MixingRazorViewsAndWebFormsMasterPagesWithASPNETMVC3.aspx

Advertisements
Categories: ASP.NET MVC
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: