MVC3 Razor Template Engine

Source: Internet
Author: User

first, Razor basic

  1. Single-line code writing

@ Code

  2, multi-line code writing

@{//@ Add a curly brace to write C # code in it.

C # code First line

C # code second line

}

  3, the Razor template engine will automatically distinguish the code block, but when the code is ambiguous, you can use parentheses to clarify

@{    " Zhang San ";} // @name Hello Error The name does not exist in the current context "name Hello" *@@ (name) Hello     

As an example, when invoking a generic method, the angle brackets cause the razor to return to the token, which can also be clarified with parentheses:

@ (html.somemethod<type> ())

For the message name, Razor uses a simple algorithm to distinguish, can adapt to most cases, when the occurrence of razor discriminant, you can use two @, @@ 来 force the output @.

  4. HTML Encoding

The default output is HTML-encoded, which is useful for preventing XSS attacks, but if I want to put a piece of code, like JavaScript code output, to the foreground, use

@Html. Raw (content) to ensure that the contents are not encoded

@{String message = "<Script>Alert‘How are you doing!‘);</Script>"; }<Span> @message</Span>//actual output of both browser source code<Span>&lt;script&gt;alert (& #39; hello!& #39;);&lt;/script&gt; </span> << Span style= "color: #800000;" >span> @Html. Raw (@message) span>//actual output to the browser source < script>alert ( Span style= "color: #000000;" > ' ); </script>  

5. Mixed code and plain text

If you want to output a piece of content to your browser in a block of code, you need to use

@: Text to output//single line

<text> want to output text </text>//can be written in multiple lines

And <text></text> can also not write in @{} code block, but @: must be written in the code block, recommended to use <text></text>

@{

var i = 1;

@:@i; For output variables

}

  6. Mvc calls the background method

Controller code:

PublicClassHomecontroller:controller {Public ActionResult Index () {viewdata[" kk "] =   Changjiang River, you are very long!< Span style= "color: #800000;" > "; return View (list< Span style= "color: #800000;" > "); } public static string< Span style= "color: #000000;" > GetName () {return  " Andy Lau  "       
public string GetName1 ()        {            return "Jacky";        }
}

View Code:

@MvcStart. Controllers.HomeController.GetName (); Invocation of a static method

@{//Non-static method call var con = new MvcStart.Controllers.HomeController (); var name1 = con. GetName1 (); } @name1

Note that the above is a static method, the static code is the simplest, directly @ Write the namespace and so on. Static methods can only be written in multiple lines and referenced by a new object.

@Url. Action ("index") This method is used to return an action path that can be used when the action path is required, such as the JavaScript src is generated by the program, and so on.

MVC3 Razor Template Engine

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.