Razor Syntax (ii)

Source: Internet
Author: User
Tags visual studio 2010

I:asp.net MVC3 changes in Visual Studio 2010

A new MVC3 project in VS2010 shows that there has been a noticeable change with the previous MVC2.

  1.asp.net MVC3 the necessary operating environment for. NET 4.0 (want to use MVC3 in 3.5, no way!).

  2. Default MVC3 template project is integrated

  3. The new Razor view engine

@{
Viewbag.title = "Home page";
}

<p>
To learn more about ASP. NET MVC visit
<a href= "Http://asp.net/mvc" title= "ASP. NET MVC Website" >
Http://asp.net/mvc
</a>.
@ServerInfo. gethtml ()
</p> 4. All documents that begin with "_"

ASP. 4 By default, all _*.cshtml documents that have access addresses that begin with "_" are rejected. About *.cshtml documentation, he is a part of the WebMatrix, which will be described in detail later with the "_" documentation.

For example, Access http://localhost:7985/_ViewPage1.cshtml.

Ii:razor View Engine-basic syntax

Infrastructure

Any part of the code that starts with @ or @{/* code body */} (without adding any spaces directly at @ and {) will be processed by the ASP. Each line of code in the @{/* Code body */} must have a ";" End, such as:

@{
var i = 10;
var y = 20;
}

And @xxx do not need to ";" As a terminator, such as:

@i Output 10

@y; Output 20;

The letters in the code area are case-sensitive.
Character type constants must be enclosed in "" For example: @{string str = "my string";}.

Note

If you need to output the "@" character on the page.
You can use HTML ASCII encoding & #64;
Of course Razor also provides intelligent analysis capabilities: If the previous word of @ characters is a non-whitespace character, ASP. NET does not process it.

such as: <p>[email protected] xx</p> output [email protected] XX.

Single-line Syntax:

@{var I = 10;}

Multi-line Syntax:

@{
var I = 10; Var y = 20;
}

1. Using local variables, razor does not support access modifiers (public,private, etc., this does not make any sense)

To define a local variable on a single line:

@{var total = 7;}
@{var mymessage = "Hello World";}

To define a local variable on multiple lines:

@{
var greeting = "Welcome to our site!";
var weekDay = DateTime.Now.DayOfWeek;
var greetingmessage = greeting + "Today is:" + weekDay;
}

Use variables in context:

<p>the value of your account is: @total </p>
<p>the value of Mymessage is: @myMessage </p>

Note: The variable stitching output.

@{var i = 10;}
<p>text @i text</p>
Outputs the text

But what if you want to output text10text?

<p>[email protected]{@i}text</p> can
<p>[email protected] text</p> will output [email protected] Text
<p>[email protected]</p> will output [email protected]
<p>text @itext </p> will error

If the output is a variable, the method name does not need to be enclosed in @{} and can take effect such as:

<p>[email protected] () text</p>

Use variable objects to write directly: @var1 @var2 @myObject. xx

  2. Using logical processing

@{
if (XX)
{
Do something
}
Else
{
Do anything
}
}

  3. In @{...} Using HTML tags internally

@{
<p>text</P>
<div>div1</div>
}

  4. In @{...} Internal output text

Use @: For single-line output:

@{
@:this is some text
@:this is text too
@:@i can also output variables
}

Multi-line output with <text/>:

@{
<text>
Tomorrow is good
Some girl is nice
</text>
}

  5. In @{...} Internal Use Notes

@{
Single-line Comment
var i = 10;
Defg
}
@* Multi-line Comment *@
@*
Multi-line comments
Multi-line comments
*@
@{
@*
Multi-line comments
Multi-line comments
*@
var i = 10; @* asdfasf *@
}
<--can also use C # default */* ...
@{
/*
Multi-line comments
*/
}

If in @{...} Internal use <!----> comments are exported to the page and processed if the @ variable is used inside <!---->:

@{
<!--time Now: @DateTime. now.tostring ()--
}
Output: <!--time now:4/9/2011 12:01--

  6. Type conversion

Asint (), Isint ()
Asbool (), Isbool ()
Asfloat (), Isfloat ()
Asdecimal (), Isdecimal ()
Asdatetime (), Isdatetime ()
ToString ()
Example:

@{
var i = "10";
}
<p> i = @i.asint () </p><!--Output i = ten---

  7. Using loops

<!--Way 1-->
@for (int i = ten; i < one; i++)
{
@:@i
}
<!--Way 2-->
@{
for (int i = ten; i < one; i++)
{
Do something
}
}
<!--while empathy--

Razor Syntax (ii)

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.