What is a View ?
- View provides the UI (User Interface) of the application to the user.
- View is used to display content of an application and also to accept user inputs (Forms).
- View uses model data to create this UI (User Interface).
- View contains both HTML markup and C# code that runs on the Web server.
- View has a file extension .cshtml
What Is Razor ?
- Razor is a syntax, based on the ASP.NET Framework that allows creating views.
- Razor is simple and easy to understand for users who are familiar with the C#.NET or VB.NET programming languages.
Following code snippet shows a simple razor view containing both HTML markups for presentation and C# code:

The MVC Framework uses a view engine to convert the code of a view into HTML markup that a browser can understand.
What Is Razor View Engine ?
- Razor View Engine is used as the default view engine by the MVC Framework.
- Razor View Engine compiles a view of your application when the view is requested for the first time.
- Razor View Engine delivers the compiled view for subsequent requests until you make changes to the view.
- Razor View Engine does not introduce a new set of programming language, but provides template markup syntax to segregate HTML markup and programming code in a view.
A Razor
- First requires identifying the server-side code from the markup code to interpret the server-side code embedded inside a view file.
- Uses the @symbol, to separate the server-side code from the markup code.
While creating a Razor view, you should consider following rules:
- Enclose code blocks between @{ and }
- Start inline expressions with @
- Variables are declared with the var keyword
- Enclose strings in quotation marks ” “
- End a Razor code statement with semicolon (;)
- Use the .cshtml extension to store a Razor view file that uses C# as the programing language
- Use the .vbhtml extension to store a Razor view file that uses VB as the programing language.
In this lecture, we will learn
- Inline expression statement.
- Declaring variable using var keyword.
- Multi statement code block.
- Multi statement code block.
- Loops
Source Code Of HomeController
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace RazorSyntaxDemo.Controllers
{
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
return View();
}
}
}
Source Code Of View – Where We Write Razor Code
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<!--Inline expression statement-->
@*<p>TODAY DATE AND TIME IS: @DateTime.Now</p>
<p>THIS YEAR IS @DateTime.Now.Year</p>*@
<!--declaring variables-->
@*@{
string name = "Learning Never Ends";
var TodaysTime = DateTime.Now.ToLongTimeString();
}
<h2>Channel name is: @name</h2>
<h2>Todays time is: @TodaysTime</h2>*@
<!--Conditional statements-->
@*@{
int marks = 75;
if(marks >= 80)
{
<h2>YOUR GRADE IS A-1</h2>
}
else
{
<h2>YOUR GRADE IS A</h2>
}
}*@
@*@{
string fruit = "orange";
switch(fruit)
{
case "banana":
<h2>YOU HAVE SELECTED BANANA</h2>
break;
case "strawberry":
<h2>YOU HAVE SELECTED STRAWBERRY</h2>
break;
case "apple":
<h2>YOU HAVE SELECTED APPLE</h2>
break;
default:
<h2>INVALID FRUIT</h2>
break;
}
}*@
@*@{
for (int i = 1; i < 10; i++)
{
<h2>@i</h2>
}
}*@
@{
string[] sports = {"FOOTBALL","CRICKET","HOCKEY","BASEBALL" };
foreach (string i in sports)
{
<h2>@i</h2>
}
}
Click Below Link to Download Notes Of This Blog
https://www.mediafire.com/file/s3i399knsgtdca7/WHAT+IS+RAZOR+IN+MVC.docx/file
Click Below Link to Download Source Code Project Of This Blog
https://www.mediafire.com/file/o2h7frhpjgpras5/RazorSyntaxDemo.rar/file
No responses yet