ASP.NET MVC 3: Using multiple view engines in same project

One of nice features of ASP.NET MVC 3 is support for multiple view engines. There will be two out-of-box view engines: one for classic ASPX-based views and one for Razor-based views. In this posting I will show you this new feature and demonstrate how to use different view engine in same project.

Currently, I think, there is no real application to tricks in this posting but you never know. When ASP.NET MVC has been available for longer period of time I think there will be need to migrate two projects that use different view engines.

Support for multiple view engines

ASP.NET MVC: Add view and select view engineHere you can see screenshot of Add View dialog where view engine dropdown is opened.

Currently there are two choices:

  • ASPX – classic ASP.NET MVC views,
  • Razor – new Razor based views.

If you select another view engine then file and master page names are changed appropriately.

In the future there will be maybe third option for Visual Basic.NET based Razor views (CSHTML means C# based views).

Using multiple view engines in same project

Now let’s make little experiment. Here is my step by step guide.

  1. Create ASP.NET MVC 3 ASPX project.
  2. Create ASP.NET MVC 3 Razor project.
  3. Copy Site.master and LogOnUserControl.ascx from ASPX project shared views folder to Razor project shared views folder.
  4. Copy About.aspx from Home views folder of ASPX project to Home views folder of Razor project.
  5. Rename About.aspx to AboutAspx.aspx.
  6. Open _Layout.cshtml and add the following line to the end of menu:
    <li>@Html.ActionLink("About ASPX", "AboutAspx", "Home")</li>
  7. Open Site.master and add the following line to the end of menu: 
    <li><%: Html.ActionLink("About ASPX", "AboutAspx", "Home")%></li>  
  8. Save both master pages.
  9. Open AboutAspx.aspx and change the text in body so you can recognize that ASPX-based view is shown.
  10. Save AboutAspx.aspx.

Now you can run your project and select About and About ASPX from main menu. If you select About you will see Razor-based view and if you select About Aspx you will see ASPX-based view.

Conclusion

Using views based on different view engines allows us to migrate ASP.NET MVC projects that are written using different view engines. As human mind is unlimited I have to suggest that it is not good idea to use different view engines in same project. Try to avoid pointless mess if you can and select view engine by trusting your common sense.

Gunnar Peipman

Gunnar Peipman is ASP.NET, Azure and SharePoint fan, Estonian Microsoft user group leader, blogger, conference speaker, teacher, and tech maniac. Since 2008 he is Microsoft MVP specialized on ASP.NET.

    3 thoughts on “ASP.NET MVC 3: Using multiple view engines in same project

    • September 5, 2010 at 3:15 pm
      Permalink

      Thanks!

      But I will wait to the time when VS2010 support razor color syntax.

    • September 7, 2010 at 2:38 pm
      Permalink

      Tumi Le, missing syntax highlight is only problem if you have visual disabilities. If you don’t then feel free to try this stuff right now and exercise how it feels to use simpler editors without syntax highlight. Some day they may save your ass. :)

    • December 6, 2010 at 8:23 pm
      Permalink

      This is even very useful when migrating just 1 project with only the ASPX view engine, when in the end you want to migrate everything to Razor.
      I am currently migrating from MVC2 to MVC3 and will use this: it allows doing all new development with Razor while existing views remain as they are until we have time or until we need to change them!
      If there was no possibility to have 2 view engines I might seriously consider to not migrate this project to Razor at all…which would be very sad.

    Leave a Reply

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