This is the 12thMVC (Model view controller) tutorial and in this article we will try to understand what is Razor in MVC 3.
What is RAZOR in MVC 3(MVC tutorial number 12)?
This is the 12thMVC (Model view controller) tutorial and in this article we will try to understand what is Razor in MVC 3.
Alternatively if you are completely new to MVC, you can also see all the MVC ASP.NET videos to come up to speed.
Introduction
Till now this article was using MVC 2 but it’s high time we
also start discussing and doing labs with new release versions of MVC
frameworks. Change is a part of human life and same stands true for MVC as well
J. So in this section let’s discuss
MVC 3 which was the next release version after MVC 2.
FYI: - The recent version is MVC4 and in the later days I will be touch basing even those versions. So control yourself and have patience.
In case you have not installed MVC 3 template, you can get it from here
In case you are feeling that whatever we have learnt in MVC 2, is it a waste?.No, not at all. On the contrary MVC 3 is backward compatible with MVC 2. So whatever you have learnt in MVC 2 still holds true in MVC 3.
Now rather than discussing about all the new features let’s focus on the biggest feature of MVC3 which I personally think is a game changer and that is RAZOR.
So what’s Razor, just to answer short and sweet, it’s a type of view for MVC. In MVC 2 the default view was ASP.NET pages i.e. Web form view. Now the problem of web form views was that it was not made thinking MVC in mind, so the syntaxes was bit heavy.
Developers demanded for a clean, light weight view and with less syntactic noise: - Answer was RAZOR.
So let’s create a simple lab to demonstrate use of Razor views.
Step 1:- Install MVC 3 and create a project using the same
Install MVC 3 template and create a project selecting the MVC 3 template below.
Step 2:- Select Razor
The next screen which pops up what kind of application you want to create.
- The Empty option creates a project with least amount of files.
- The Internet Application option creates a shell application which includes user registration and authentication, navigation, and a consistent visual style.
- The Intranet Application option is very much same as Internet Application with the only difference that the authentication takes place through domain/Active Directory infrastructure.
For now let’s keep life simple and let’s select the empty option. The second thing which we need to select is what kind of view we want, so let’s select Razor and move ahead.
Once the project is created you can see the Razor file with the name “.cshtml”.
Step 3:- Practice Razor syntaxes
Now that we have the basic project ready lets run through some common razor syntaxes and try to get a feel how easy RAZOR is as compared to ASPX views.
Practice 1 :- Single line code
If you want to just display a simple variable you can do something as shown below. All razor syntaxes start with @. If you have just single line of code you do not need “{“. Razor figures out the ending logically.
Todays date @DateTime.Now
If you compare the above syntax with ASPX view you need to type the below code, so isn’t the syntax much simpler,neat and light weight.
<%=DateTime.Now%>
Practice 2 :- Multiple lines of code
If you have multiple line of code you can use “@” followed by “{“as shown in the below code snippet.
@{
List<string> obj = new List<string>();
obj.Add("Mumbai");
obj.Add("Pune");
obj.Add("Banglore");
obj.Add("Lucknow");
}
Practice 3 :- Foreach loop and IF conditions
For loops and if conditions again become simpler as shown in the below lines of code.
@foreach (string o in obj)
{
@o <br />
}
@if (DateTime.Now.Year.Equals(2011))
{
// Some code here
}
Practice 4:- Do not worry about @
If you are thinking will razor confuse with @ of Razor and @ of your email address, do not worry razor understands the difference. For instance in the below line the first line razor will execute as a code and the second line of code he understands it’s just an email address.
@DateTime.Now
questpond@yahoo.com
Practice 5:- To display @
In case you want to display “@” just type it twice as shown in the below code snippet the display will be something as shown in the image below.
Tweet me @@Shivkoirala
Practice 6:- HTML display with razor
In case you want to display HTML on the browser. For instance below is a simple variable called as link which has HTML code. I am displaying the variable data on the browser.
@{
var link = "<a href='http://www.questpond.com'>Click here</a>";
}
@link;
If you execute the above code you would be surprised to see that it does not display as HTML but as a simple display as shown below. Now that’s not what we expect? , we were expecting a proper HTML display. This is done by razor to avoid XSS attacks (I will discuss about the same in later sections).
But no worries razor team has taken care of it. You can use the “Html.Raw” to display the same as shown in the below code snippet.
@{
var link = "<a href='http://www.questpond.com'>Click here</a>";
}
@Html.Raw(link);
By profession I am a trainer if you are interested in C# training, you can visit me at C# and .NET training.