Razor in ASP.NET MVC 3.0

Razor in ASP.NET MVC 3.0


ASP.NET MVC 3.0, offers a new View Engine: Razor, method used and not very different from the past, but the wording has become more simple and neat; the past troublesome <%%>, is reduced to a short @, if you need to declare multi-line, only need to use the {@} wrapped up on it! For me, the best part is when typesetting pretty super multi-XD

Below is the basic Razor View:

@model statement page Model type

MasterPage name (if you do not apply is null) @Layout statement you want to apply the

@ View.Title page title

image

In addition to page statement, you can declare all the pages you want to apply the Layout with a more simple way, that is to use _ViewStart.cshtml to declare all of the default Web site to apply the page. _ViewStart.cshtml only affect him the same layer under the directory, so if you have to use area, can Views under the area, the re-designated _ViewStart.cshtml.

 SNAGHTML53f844

_ViewStart.cshtml addition to being used to specify Layout outside, something to deal with common can also be set here, for example: set CacheControl

SNAGHTML173cf85

In the Master Page (Razor In fact, the general pages and Master Page no particular difference, but used to call the page for registering in this way), the previously used ContentPlaceHolder do different blocks are separated, and the Razor it is based on the Body as cutting and Section

image

When applied, the contents of Body does not require additional treatment, and Section @section only need to wrap! (To be designated Section Name)

image

Razor many uses, such as @function, the page can use a custom function:


@functions {
    public static IHtmlString Print(int times, Func
  
  
   
    template) {
        StringBuilder builder = new StringBuilder();
        for(int i = 0; i < times; i++) {
            builder.Append(template(i));
        }
        return new HtmlString(builder.ToString());
    }
}

@{
    View.Title = "Home Page";
}


   
   

@View.Message

To learn more about ASP.NET MVC visit http://asp.net/mvc.

@Print(10, @ item: #@item);

Related Links:

Introducing “Razor” – a new view engine for ASP.NET

ASP.NET MVC 3: Layouts with Razor

Inside Razor - Part 3 – Templates

Dotblogs label: Razor, ASP.NET MVC

Original: Big Box  Razor in ASP.NET MVC 3.0


Guess you like

Origin www.cnblogs.com/chinatrump/p/11458459.html