Razor Syntax

Posted on Updated on

set-razorRazor Syntax

Razor is a markup syntax that is basically used to embed server-based code in a web page. It has all the strength of traditional ASP.Net markup and is easy to learn and use. It resembles with ASP & PHP and supports C# as well as Visual Basic for coding.

 A web-page mainly have two type of contents-

  1. Client Content
  2. Server Code

Razor syntax facilitates to add the server code to the client content. If a page has any server code, the browser first executes that code and the sends the page to the browser. A server code can be a very simple single line code to a very complex code like having access to a database. A server code can   dynamically create a client content and then send it to the browser along with the static HTML that the page might contain. From browser’s outlook, the client code generated by the server code is similar to any other client content.

The files which have Razor syntax are saved with the file extension (.cshtml or .vbhtml). The server identifies these extensions, runs the code that is marked with Razor syntax and then sends the page to the browser.

 Syntax Rules for C#

  1. Razor code blocks are enclosed in @ {………}
  2. Inline expressions like functions and variables start with @
  3. Termination of statement is denoted by semicolon (;)
  4. Variables declaration is preceded with var keyword
  5. While writing the Strings, they have to be enclosed in quotation marks
  6. Code generated by C# is case sensitive
  7. The files are to be saved with .cshtml

Syntax Rules for VB

  1. Code blocks are enclosed in @Code…….. End Code
  2. Inline expressions like functions and variables start with @
  3. Variables are declared with DIM keyword
  4. Strings are enclosed in quotation marks
  5. Code generated by VB is not case-sensitive
  6. The files are to be saved with .vbhtml

Razor Helpers

ASP.Net has provided some helper components that can be easily accessed by single line of razor code.

User defined helper codes can also be developed beside built-in codes. Some of the useful helpers are-

 Web Grid

 Web Graphics

 Google Analytics

 Facebook Integration

 Twitter Integration

 Sending Email

 Validation

Simple Code to create a web page using Razor Syntax

<!– Single statement blocks  –>

@{ var total = 7; }

@{ var myMessage = “Hello World”; }

<!– Inline expressions –>

<p>The value of your account is: @total </p>

<p>The value of myMessage is: @myMessage</p>

<!– Multi-statement block –>

@{

var greeting = “Welcome to our site!”;

var weekDay = DateTime.Now.DayOfWeek;

var greetingMessage = greeting + ” Today is: ” + weekDay;

}

<p>The greeting is: @greetingMessage</p>

Output

*****************************************************************************************************

For more details and queries please feel free to email, visit or call us. Wishing you the very best for all your future endeavors.
Helpline: 9814666333, 8699444666
Email: info@technocampus.co.in

Please fill the form and we shall contact you soon.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s