Using Razor View Engine in ASP.Net web Applications

The view engine is responsible for creating HTML from your views. Views are usually some kind of mix-up of HTML and a programming language. The choice of view engine is important, because the feature sets of view engines are quite different. The Razor syntax is based on the C# programming language, and that’s the language that’s used most often with ASP.NET Web Pages. However, the Razor syntax also supports the Visual Basic language, and everything you see you can also do in Visual Basic.

Razor is a simple-syntax view engine and was released as part of MVC 3.The Razor syntax is a template markup syntax, based on the C# programming language. The idea behind Razor is to provide an optimized syntax for HTML generation using a code-focused templating approach, with minimal transition between HTML and code.

Videos: If you are interested to learn more about Razor view Engine then you can visit my video series on YouTube (FREE).

Use Variables
To create a variable in Razor view, we need to declare it by using @{...} block. To display/print its value just need to write variable name followed by @ sign like below example. To display static text with in razor view use @:text. You can use any HTML tags with in your razor view just like >br< in below example.

// Declare Variable
	var name = "Nilay";

@if (Name.Length == 0)
	// If its 0-length then display messing message
	@:Missing Name!
	// If its exist the display its value.
	@:Your name is @name;

ViewBeg & ViewData
View bag and view data both used to pass data from controller to view. View Bag provide dynamic properties to store & pull values from controller to view Note that it is a dynamic feature that was introduce in c# 4 Which allow dynamically add properties to our object means that here heading is not inbuilt property it was added by us, we can choose any name. Both of them does not generate any compile time errors if we are misspell any property or key. ViewBeg data are stored into ViewData’s dictionary internally. Here is the example in which we will set values to VewBeg and ViewData from Controller and use those values in Razor view.


public ActionResult SayHello()
	ViewBeg.HelloFromBeg = "Hello from ViewBeg";
	ViewData["HelloFromData"] = "Hello From ViewData";
	return view();



Generate Hyperlinks
Some time we need to generate link to any specific controller from view. HTML Helper class provides various methods for generating html markup for our page. Such as hyperlinks, forms, etc.
ActionLink() is used to generate hyperlinks. It takes various attributes such as Text to be display on link, Action name, Controller name, parameters for that action, and other HTML attributes.

ActionLink in Razor view

Learn More
If you want to learn more about Razor view then you can visit my video playlist on YouTube, which is absolutely free.

  • Printing Variables
  • Create Code Block
  • ViewBag and ViewData
  • HTML Helper
  • URL Helper
  • Manage Forms
  • Partial & Strongly typed Views
  • Layout and Sections
  • _ViewState.cshtml file

Videos: If you are interested to learn more about Razor view Engine then you can visit my video series on YouTube (FREE).

Leave a Reply

Your email address will not be published. Required fields are marked *