Tips to program in ASP.NET using Razor


Here we are going to learn about programming in asp.net using Razor syntax. Some tips to write the program are given below, will be helpful for beginners. It is about how to add codes, variables and using literal strings in Razor.

Find Tips to program in ASP.NET using Razor


Use @ character before code block:

Before every Razor code block use the @ character

@{ var Number = 123; }
@{ var Literal_String = "Hello World"; }

Use @ character before a variable:

You should use the @ character for the variables you use in the inline expression.
<p>The value of number is: @Number</p>
<p>Your string is : @Literal_String</p>

Enclose code blocks in braces:

As in the first example you should write code inside the curly braces
@{
var Number = 123;
var Literal_String = "Hello World";
}
<p>Number and string: @Number and @Literal_String</p>

Inside a block, you end each code statement with a semicolon:

As like asp.net the codes should be end with semicolon
@{
var Number = 123;
var Literal_String = "Hello World";
}

Inline expression don't need semicolon:

Code inside the HTML tags don't need any semicolon
<p>The number is: @Number</p>

Use variables to store values:

We can store any datatype into a variable created using var keyword

@{ var Today_Date = DateTime.Now; }

Razor Code is case sensitive:

Razor is case sensitive so the name's with small letters differ from capitals.
@{
var Number = 12;
var number = 23;
}

in the above example both are different variables

Strings that contains a backslash, double quotation marks, uses a verbatim(@) symbol.

If you're using backslash(\) or double quotation marks in code block should be started with the @ character
@{ var Folder_Name = @"C:\MyFolder\"; }

@{ var Using_Double_Quote = @"The person said: ""Hello, today is Monday."""; }

Comments:

Razor uses @* for start of a command and *@ for end a command. It can be used for both single line and multiple line commands
@* command example *@


Comments

Guest Author: Naresh Kumar31 May 2012

That is good but not enough for study,,,,
can u plz elaborate this more

Guest Author: Hanum11 Jun 2012

Hi, If you are talking about sacreh in your own website then you must be ensured that the keywords u r putting in database, if yes then use a select command query and it is displayed result.

Author: Phagu Mahato26 Dec 2013 Member Level: Gold   Points : 6

The Razor syntax gives you all the power of ASP.NET, but using a simplified syntax that's easier to learn if you're a beginner and that makes you more productive if you're an expert. Even though this syntax is simple to use, its family relationship to ASP.NET and the .NET Framework means that as your websites become more sophisticated, A few tips that you absolutely need to know as you start writing ASP.NET server code using the Razor syntax.
[1]You add code to a page using the @ character
Example


@Code Dim total = 7 End Code
@Code Dim myMessage = "Hello ASP.NET World" End Code

The value of your account is: @total


The value of myMessage is: @msg



@Code
Dim greeting = "Welcome to our personal website!"
Dim weekDay = DateTime.Now.DayOfWeek
Dim greetingMessage = greeting & " Today is: " & weekDay.ToString()
End Code

2. You enclose code blocks with Code...End Code
Example

@Code
Dim outsideTemp = 79
Dim weatherMessage = "This it is " & outsideTemp & " degrees"
End Code



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: