[ASP. NET MVC] ASP. NET MVC5 series--razor syntax

Source: Internet
Author: User
Tags valid email address

Directory

Concept

Razor syntax

Summarize

Series Articles

[ASP. NET MVC] ASP. MVC5 Series--First project

[ASP. NET MVC] ASP. MVC5 Series--Add view

[ASP. NET MVC] ASP. MVC5 Series--Add model

[ASP. NET MVC] ASP. MVC5 Series--accessing data from the controller in the model

[ASP. NET MVC] ASP. MVC5 Series--Add data

[ASP. NET MVC] ASP. MVC5 Series--Adding validation rules to the model

[ASP. NET MVC] ASP. MVC5 Series--Edit, delete and detail view

Concept

The Razor view engine is the newly expanded content in ASP. NET MVC3, and is also its default view engine. There is another Web Forms view engine. The previous article shows that the view is created in ASP. MVC5, which uses the Razor view engine by default. and the real one. Remember the previous version, you can also let the developer choose whether to use the razor or Webfroms view engine.

Razor provides a streamlined syntax for view representations, minimizing syntax and extra characters. This effectively reduces grammatical barriers and does not have new syntax rules in the View markup language.

Razor supports two file types, the. cshtml and. Vbhtml, where the. cshtml server code uses the C # syntax. Vbhtml's server code uses the vb.net syntax.

Razor syntax

The core conversion character of Razor is the "at" sign (@).

The character function

Used as a token-code conversion character.

Code-the converted character of the token.

There are two basic types of conversions: code expressions and code blocks. The value of the expression is calculated and the value is then written to the response.

Code Expression

An implicit code expression

Example:

1         @{string strName = "Wofly";} 2         < H1 > The length of the boy's name is  @strName. Length chars.</H1>

Results

Razor is smart to know that the space character after the expression is not a valid identifier, so you can go back to the markup language smoothly.

Show Code expression

Razor's ability to automatically transfer tags from code can sometimes bring two semantics, such as the following razor fragment:

1         @{string"MyApp";} 2         <span> @rootNameSpace .models</span>

The result you want to output is:

<span>MyApp.Models</span>

An error occurred at compile time, suggesting that the string has no models property. In this case, Razor doesn't understand what we want to do, and we think @rootnamespace is a code expression. So what's the solution? Razor can display a code expression by enclosing an expression in parentheses:

<span>@ (RootNamespace). Models</span>

This tells Razor,. Models are literal literals, not part of an expression.
How does the @ symbol differ from the @ in the mailbox?

For example:

<span>[email protected]163.com</span>

You will find that the shape of the above code is not an error, razor How to do it?

Razor uses a simple algorithm to determine whether a string that looks like a mailbox address is a valid e-mail address. Although it is not perfect, it can be used in most cases. In some special cases, a valid email address may not show up, which is an @ sign that can be escaped with two @@ 符号.

What if we want such a string as an expression?

If we have one of these list items:

<li>[email protected]</li>

In this particular case, the expression is parsed into an e-mail address, so razor prints it verbatim. But the result we want is:

<li>item_3</li>

So in this case, we can still use parentheses to indicate what we want.

<li>[email protected] (item. Length) </li>

If you want to output a character at the beginning of @, you can use @@ 转义 escape the @ character.

HTML encoding

In many cases it is necessary to use a view to display the user's input, such as some Web site comments. So there is always a potential cross-site Scripting injection Attack (XSS). These, razor also think for us, razor expression is automatically encoded with HTML.

1         @{string"<script>alert (' warning ');</script>";} 2         <span> @strAlert </span>

Looking at the generated HTML, you'll find that the tags are HTML-encoded:

< span > &lt; Script&gt; Alert (& #39; warning & #39;);  &lt; /script&gt; </ span >

Page display

No coding code Expression

If we do want the browser to parse into an HTML tag, we need to return an instance of the System.Web.IHtmlString object, in which case razor does not encode it. For example, an HTML helper class. Also we can use the Html.raw method.

1@{string Stralert = "<Script>Alert ('Warning');</Script>";}2         <span>@Html. Raw (Stralert)</span>

The pop-up box appears when you browse the view.
It is also important to note that when assigning a user-supplied value to a variable in JS, use the JS string encoding rather than just the HTML encoding. That is, if the user input is encoded using the @ajax.javascriptstringencode method.

code block

The code block is a simple code execution section.

For example

1         @{2            int x = 123; 3             String y = "wofly"; 4         }

The above code block defines the x and string type y of the variable int type, and if you want to output them, you can use @x and @y.

Mix text and labels with

        @foreach (var item in models) {2         <span> @item. Name</span>3}       

Mixed code and pure text

1 @if (true)2{3     <text> This is text </text >4 }

Server-side annotations can use @**@ to annotate unwanted code.

@* @if (true) {    <text> This is text </text>}*@

Calling a generic method

1  @ (html.somemethod<t> ())

Summarize

Here is a brief introduction to the two basic types of razor: code expressions and code blocks. Grammar is not as difficult as it is thought. The key is the @ character.

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.