Razor syntax is a simple programming syntax for embedding server-based code in a web page. In a web page that uses the Razor syntax, there are two kinds of content:
client content and
server code. Client content is the stuff you're used to in web pages: HTML markup (elements), style information such as CSS, client script such as JavaScript, and plain text.
Razor syntax lets you add server code to this client content. If there's server code in the page, the server runs that code first, before it sends the page to the browser. By running on the server, the code can perform tasks that can be a lot more complex to do using client content alone, like accessing server-based databases. Most importantly, server code can dynamically create client content—it can generate HTML markup or other content on the fly and then send it to the browser along with any static HTML that the page might contain.
ASP.NET web pages that include the Razor syntax have a special file extension (.cshtml or .vbhtml). The server recognizes these extensions, runs the code that's marked with Razor syntax, and then sends the page to the browser.
8 Programming Tips for Writing the Razor Syntax:
1) You add code to a page using the @ character
The @ character starts inline expressions, single statement blocks, and multi-statement blocks:
Example :
<!-- Inline expression -->
<p>The value of your account is: @total </p>
<!-- Single statement block. -->
@{ var myMessage = "Hello World"; }
<p>The value of myMessage is: @myMessage</p>
<!-- Multi-statement block. -->
@{
var weekDay = DateTime.Now.DayOfWeek;
var greetingMessage = "Welcome all "+ " Today is: " + weekDay;
}
<p>The greeting is: @greetingMessage</p>
2) You enclose code blocks in braces
Example:
<!-- Multi-statement block. -->
@{
var weekDay = DateTime.Now.DayOfWeek;
var greetingMessage = "Welcome all "+ " Today is: " + weekDay;
}
Observe the above code which is written in { } braces
3) Inside a block, you end each code statement with a semicolon.
Example:
< !-- Multi-statement block. -->
@{
var weekDay = DateTime.Now.DayOfWeek;
var greetingMessage = "Welcome all "+ " Today is: " + weekDay;
}
Observe the above code where each statement ended with Semicolon.
Note: Inline expressions do not end with a semicolon
4) You use variables to store values
You can store values in a variable, including strings, numbers, and dates, etc. You create a new variable using the var keyword. You can insert variable values directly in a page using @.
Example:
<!-- Multi-statement block. -->
@{
var weekDay = "Sunday";
var Date = "23/09/2010";
var greetingMessage = "Welcome all "+ " Today is: " + weekDay;
}
5) You enclose literal string values in double quotation marks
To specify a string, you enclose it in double quotation marks:
@ { var myString = "This is a string literal"; }
If the string contains a backslash character (\) and double quotation marks, use a verbatim string literal that is prefixed with the @ operator.
<!-- Embedding a backslash in a string -->
@{ var myFilePath = @"C:\MyFolder\"; }
<p>The path is: @myFilePath</p>
To embed double quotation marks, use a verbatim string literal and repeat the quotation marks:
<!-- Embedding double quotation marks in a string -->
@{ var string1 = @"The person said: ""Hello, today is Monday."""; }
<p>@string1</p>
6) Code is case sensitive
The following lines of code create two different variables:
@{
var lastName = "sri";
var LastName = "kiran";
}
7) Much of your coding involves objects
In today's Programming world everything is treated as object and then it is programmed.Even the Razor syntax follows the same.For example: a page is a object,textbox is a object ,Request is a object.Using the properties of Object we can access the objects data and using objects methods we can perform operations on it.
Example:
"Request" is a Object and we can access the properties of it such as URL,RequestType etc.
<p>@Request.Url</p> Will print the Url of the Requested page
8) You can write code that makes decisions
A key feature of dynamic web pages is that you can determine what to do based on conditions. The most common way to do this is with the if statement (and optional else statement).
Example:
@{
var result = "";
if(IsPost)
{
result "This page was posted using the Submit button.";
}
else
{
result = "This was the first request for this page.";
}
}
So these are the rules to write a code in Razor syntax.Once you know the above rules, you can write server code using Razor syntax and with the help of Helpers.
Hope You people liked the Content :)