Understanding the Razor View engine in ASP.

Source: Internet
Author: User

What is Razor?

Razor is a markup syntax that allows you to embed server-based code (C #) into a Web page, a template engine that Microsoft has introduced for ASP.

C # 's main Razor syntax rules
    • Razor code encapsulated in @{...}
    • In-line expressions (variables and functions) begin with @
    • The code statement ends with a semicolon
    • string surrounded by quotation marks
    • C # code is case sensitive
    • C # files have the. cshtml extension.
<!--single-line code block -@{var mymessage = "Hello World";}<!--in-line expressions or variables -<P>The value of Mymessage is: @myMessage</P> <!--multi-line statement code block -@{var greeting = "Welcome to our site!"; var WeekDay = Datetime.now.dayofweek;var GreetingMessage = greeting + "Here's Huston it is:" + weekDay;}<P>The greeting is: @greetingMessage</P>

Dealing with objects

Server code often involves objects.

The "Date" object is a typical ASP. NET built-in object, but you can also define your own object, a Web page, a text box, a file, or a database record, and so on.

Objects can have methods that can be executed. Database records can provide a "save" method, the image object can have a "rotation" method, the E-mail object can provide a "send" method, and so on.

Objects can also have properties that describe their characteristics. Database records can have FirstName and LastName properties.

The ASP. NET Date object has the Now property (written as Date.now), and the now property has the day property (written as Date.Now.Day). The following example shows how to access some properties of a Date object:

<TableBorder= "1"><TR><thwidth= "100px">Name</th><TDwidth= "100px">Value</TD></TR><TR><TD>Day</TD><TD>@DateTime. Now.day</TD></TR><TR><TD>Hour</TD><TD>@DateTime. Now.hour</TD></TR><TR><TD>Minute</TD><TD>@DateTime. Now.minute</TD></TR><TR><TD>Second</TD><TD>@DateTime. Now.second</TD></TR></TD></Table>
Read user input

Another important feature of dynamic Web pages is the reading of user input.

The input is read by the request[] function and tested by the IsPost condition:

@{var totalmessage = ""; if (IsPost) {var num1 = request["Text1"];    var num2 = request["Text2"]; var total = Num1. Asint () + num2.    Asint ();    Totalmessage = "Total =" + total; }}<HTML><Bodystyle= "Background-color:beige; Font-family:verdana, Arial;"><formAction=""Method= "POST"><P><label for= "Text1">First Number:</label><BR><inputtype= "text"name= "Text1" /></P><P><label for= "Text2">Second Number:</label><BR><inputtype= "text"name= "Text2" /></P><P><inputtype= "Submit"value= "Add" /></P></form><P>@totalMessage</P></Body></HTML>
converting data types

It is sometimes useful to convert one data type to another.

The most common example is converting a string input to another type, such as an integer or a date.

As a rule, user input becomes a string, even if the user enters a number. Therefore, the numeric input value must be converted to a number before it is used for calculation.

The following is a list of common conversion methods:

Method Description Example
  • Asint ()
  • Isint ()
Converts a string to an integer. if (Mystring.isint ()) {Myint=mystring.asint ();}
  • Asfloat ()
  • Isfloat ()
Converts a string to a floating-point number. if (Mystring.isfloat ()) {myfloat=mystring.asfloat ();}
  • Asdecimal ()
  • Isdecimal ()
Converts a string to a decimal number. if (Mystring.isdecimal ()) {Mydec=mystring.asdecimal ();}
  • Asdatetime ()
  • Isdatetime ()
Convert a string to an ASP. NET DateTime Type Mystring= "10/10/2012"; Mydate=mystring.asdatetime ();
  • Asbool ()
  • Isbool ()
Converts a string to a logical value. Mystring= "True"; Mybool=mystring.asbool ();
ToString () Converts any data type to a string. myint=1234; Mystring=myint.tostring ();

Understanding the Razor View engine in ASP.

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.