ASP.NET MVC 3 Beta: View start files for Razor view engine

To follow better DRY (Don’t Repeat Yourself) principle Razor views support now defaults that are applied to all views in ASP.NET MVC web application. Default can be applied by special file so you have one place where everything common is defined. In this posting I will show you how defaults work and how to use them.

View start files

ViewStart fileIf you create new ASP.NET MVC 3 Beta web application you will find there file called _ViewStart.cshtml. In VB.NET projects this file is called _ViewStart.vbhtml.

By default this file is very short and simple containing only definition for layout page (ASPX synonym for layout page is master page). This page is run before the code of requested view is run.

_ViewStart.cshtml has also scope – it works for views that are located under the folder where _ViewStart.cshtml is. Views in other folders (by example views in other areas if you are using areas) are not affected by  _ViewStart.cshtml in /Views/ folder.

Image on right shows how _ViewStart.cshtml is located in default internet application project. By example, it covers all views in Home and Shared folder but not views that have restricted access (_Layout.cshtml cannot be accessed directly).

Of course, we can add more functionality to _ViewStart.cshtml if we need but you have to be sure that this is something that cannot be easily done using layout pages and this is something that must be applied to views.

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.

    2 thoughts on “ASP.NET MVC 3 Beta: View start files for Razor view engine

    • October 29, 2010 at 1:29 pm
      Permalink

      I found 2 bugs in Razor view-engine.

      1. I can’t write any inline-templates in _ViewStart, cause
      WriteLitralTo is not defined in ViewStartPage class
      2. Translator inserts unnecessary whitespace writes

      For ex:

      _ViewStart.cshtml

      @{
      Func intFormatter = @- @item -;
      }

      That code translates to:

      public class _ViewStart_cshtml : System.Web.Mvc.ViewStartPage {

      public override void Execute() {
      Func intFormatter =item => new
      System.Web.WebPages.HelperResult(__razor_template_writer => {
      WriteLiteralTo(@__razor_template_writer, ” “);
      WriteLiteralTo(@__razor_template_writer, “- “);
      WriteTo(@__razor_template_writer, item);
      WriteLiteralTo(@__razor_template_writer, ” -“);
      }
      }
      }

      So:

      (1)

      CS0103: The name ‘WriteLiteralTo’ does not exist in the current context

      WriteLiteralTo is defined in System.Web.WebPages.WebPageBase, but
      StartPage does not inherited from it.

      (2)

      Why these unnecessary WriteLiteralTo calls (” “)? – there is no in my
      code. And these unnecessery calls occurs all the time even in other
      views! Why? I don’t need them!

      If I change the code, by replacing single space by other spaces (for
      ex, three tabs) between = and @:

      Func intFormatter = @- @item -;

      translator generates:

      WriteLiteralTo(@__razor_template_writer, “\t\t\t”);
      WriteLiteralTo(@__razor_template_writer, “- “);

      so, it generates writes for whitespaces BEFORE @, from code context!
      why? Is this bug?

      Is this behaivor will be changed to release?
      How to decide these problems?

    • December 22, 2010 at 1:58 pm
      Permalink

      Nice feature.

      Could you add the ViewBag to the _ViewStart fnuctionality, since it would be nice to be able to set default values for cross view ViewBag properties. Also this would help getting rid of late binding issues that are cuased due to missing ViewBag properties => more stability

    Leave a Reply

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