ASP.NET MVC 3 Beta Support for Code Runs before Views and Strongly Typed Models in Razor Views

Code that Runs Before Views Run

In ASP.NET MVC 3 Beta, we can add a file named _viewstart.cshtml in the views directory  and this will be shared among the all views with in the Views directory. The below code in the _viewstart.cshtml, sets the Layout page for every Views in the Views folder


  1. @{
  2.     Layout = "~/Views/Shared/_Layout.cshtml";
  3. }


You can put a _viewstart.cshtml file on the any subfolders of Views folder and it will be shared only for that sub folder.


Specify Strongly Typed Models in Razor Views

In ASP.NET MVC 3 Beta, we can simply specify the strongly typed model using the following syntax

  1. @model Models.RegisterModel


In the above syntax, we have specified RegisterModel in the Models namespace as strongly typed model for our Razor View.

1 Comment

  • 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:


    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, " -");



    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.


    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?

Comments have been disabled for this content.