ASP. NET MVC Learning Note-3. Razor syntax

Source: Internet
Author: User

Razor syntax is a programming syntax for server-based code embedded in Web pages. Web pages that use the Razor syntax include two different types of content: Client content and server content. Client content is commonly used in Web pages, such as HTML markup (elements), CSS styles, plain text, and client-side scripting, such as JavaScript.

The razor syntax allows you to add server code to the client content. If the page includes server code, the server code is run first, and then the page is sent to the browser. Running on a server is more complex than just displaying client content, such as accessing the server's database operations. More importantly, server code can dynamically generate client content, such as creating HTML tags or other content, and sending it to the browser along with static HTML files. From a browser's point of view, the content generated by server code is no different from other content. As you already know, the server code required is very simple.

An ASP. NET page that contains the razor syntax has a special file suffix name (cshtml or vbhtml). The server is able to identify these files and run the server code in them and then send the content to the browser.

Razor syntax is based on ASP. NET, it runs on Microsoft. Netframwork. Netframwork is a large, comprehensive programming framework that can develop any type of computer program. Asp. NET is just a section of the. Netframwork framework, which is dedicated to creating Web applications. Developers have created many large, high-traffic site applications with ASP. (At any time, you see a file that contains the. aspx suffix name in the URL of the Web site, which is implemented using ASP.)

Razor syntax allows you to better use ASP, concise syntax allows beginners to learn better, let technical experts improve work efficiency. Even if the syntax is simple, its relationship with the ASP. NET Framework can give you more support when the site becomes more and more complex.

In the server code block, you need to output text or HTML markup onto the page. If your server code includes text instead of code, you need to render the text information as it is. Asp. NET distinguishes between code and text using several ways:

Text information

Enclose text information in HTML elements, such as <p></p> or <em></em>.

Text, HTML elements, and server code representations can all be enclosed in HTML elements. When ASP. NET parses an HTML tag, it renders its contained elements and text directly, and resolves the server code expression.

Using the operator "@:" or the <text> element

@: Ability to output single-line text containing plain text or HTML tags (< br/>) that do not require matching. <text> can output multiple lines of text. You can use this method when you don't need to render some HTML markup. For example,,<br/> is a direct line change.

When you need to output multiple lines of text, you can add the "@:" operator at the beginning of each line or enclose it in the <text> element.

When a line of text is enclosed in the <text> element, ASP. NET can recognize the "@:" operator and the <text> element so that they do not appear.

Spaces and line breaks

The statement can include spaces and line breaks, but must end with a semicolon. However, there is no line break in the middle of a text string, and you can add an @ operator at the beginning of the string when the text string needs to be wrapped.

Comments

Razor comments are processed (removed) on the server before the page is sent to the browser. You can use @**@ to comment out one or more lines of code. In the razor code block, you can also use annotation methods in programming languages, such as "//" and "/**/" in C #, and you can annotate HTML elements using <!---->, but HTML annotation information is presented to the Web page and can be viewed on the source code of the Web page.

Variable

Variable names must start with a letter and cannot contain spaces or reserved keywords

You do not need to specify the type, ASP. NET resolves its type based on the data stored in the variable, so you can use the var keyword when declaring a variable.

Type conversions

Asint (), Isint (): Converts a string to a corresponding shape, and determines whether it can be converted to an orthopedic

Asbool (), Isbool (): Converts the string to the corresponding Boolean shape, and determines whether it can be converted to a Boolean

Asfloat (), Isfloat (): Converts the string to the corresponding float, judging whether it can be converted to a floating-point shape

Asdecimal (), Isdecimal (): Converts the string to the corresponding decimal number, and determines whether it can be converted to a decimal number

Asdatetime (), Isdatetime (): Converts a string to the corresponding date type and determines whether it can be converted to a date type

ToString (): Convert any type to string type

Operator

+ 、-、 *,/: Numeric operation

=: assignment operation

= =: Judging the equality operation

! =: Judging unequal operations

<, >, <=, >=: Less than, greater than, less than or equal to, greater than or equal to

+: String connection operation

+ =,-=: self-increment, self-reduction

.: Used to differentiate objects and their properties and methods

(): Used for group expressions and parameters passed to methods

[]: Gets the values in the array or list

!: Take counter action

&&, | | : Logical And, logical, or

File and folder paths

URL begins with a domain name (http://www.example) or server name (http://localhost)

The URL represents the physical path of the host computer (Http://myserver-->c:\websites\mywebsite)

The code uses a relative path to represent the shorthand for the full path, which includes the department behind the domain or server name in the URL.

Http://mycompanyserver/humanresources/CompanyPolicy.html

Servername:mycompanyserver

VirtualPath:/humanresources/companypolicy.html

Physical path: C:\mywebsites\humanresources\CompanyPolicy.html

Path conversion

Server.MapPath (virtualpath): Converting a relative path to a physical path

Relative root path: ~

ASP. NET MVC Learning Note-3. Razor syntax

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.