In this article I will with an example, how to get started with your first application using Razor View Engine in ASP.Net MVC 5.
This article will provide step by step guide to create a simple yet educational program which helps you create your first application using Razor View Engine in ASP.Net MVC 5.
 
 
Creating a new ASP.Net MVC 5 Project
Let’s get started with creating your first ASP.Net MVC 5 Project.
1. Open Visual Studio and from Start section click on New Project.
Getting Started with Razor View Engine in ASP.Net MVC 5
 
2. From the New Project Dialog window, select ASP.Net Web Application option. Then you need to set a suitable Name for your project and also you can set its Location where you want to create the Project.
Getting Started with Razor View Engine in ASP.Net MVC 5
 
3. From the new ASP.Net Project Dialog, select Empty Template and make sure the MVC CheckBox is checked as shown below.
For this sample, you can uncheck the Microsoft Azure’s “Host in the cloud” option.
Getting Started with Razor View Engine in ASP.Net MVC 5
 
4. Your first Hello World MVC Project is now ready and you should see the following folders and files in your Solution Explorer window.
Getting Started with Razor View Engine in ASP.Net MVC 5
 
 
Adding Controller to the ASP.Net MVC 5 Project
1. Inside the Solution Explorer window, Right Click on the Controllers folder and then click on Add and then Controller option of the Context Menu.
Getting Started with Razor View Engine in ASP.Net MVC 5
 
2. From the Add Scaffold Dialog window, select the “MVC5 Controller – Empty” option and click Add button.
Getting Started with Razor View Engine in ASP.Net MVC 5
 
3. You will now be asked to provide a suitable Name to the new Controller.
Getting Started with Razor View Engine in ASP.Net MVC 5
 
Once you click add, the following Controller is created. The default Action is “Index”.
Getting Started with Razor View Engine in ASP.Net MVC 5
 
 
Adding View associated with the Controller to the ASP.Net MVC 5 Project
1. Now you will need to Right Click inside the Controller class and click on the Add View option in order to create a View for the Controller.
Getting Started with Razor View Engine in ASP.Net MVC 5
 
2. You will need to provide a suitable Name to the View. This article does not use Layout Page and hence the “Use a layout page” CheckBox needs to be kept unchecked.
Getting Started with Razor View Engine in ASP.Net MVC 5
 
Once you click Add, the following View is created.
Getting Started with Razor View Engine in ASP.Net MVC 5
 
 
Configuring the Routes
The last important part is to configure the Routes in the RouteConfig.cs file.
1. Open the RouteConfig.cs class from the Solution Explorer window.
Getting Started with Razor View Engine in ASP.Net MVC 5
 
2. You will need to make sure that the Controller property is set to “Home” i.e. the Name of the Controller while the Action property is set to “Index” i.e. the Name of the Action method.
Getting Started with Razor View Engine in ASP.Net MVC 5
 
3. Now press F5 to run the Application and you should see a blank page in browser.
Getting Started with Razor View Engine in ASP.Net MVC 5
 
 
Displaying a Message from Controller to View in ASP.Net MVC 5 Project
1. Inside the HomeController’s Index method, a ViewBag property named “Message” is set.
Getting Started with Razor View Engine in ASP.Net MVC 5
 
2. The ViewBag property is displayed in the View named “Index” as shown below.
Getting Started with Razor View Engine in ASP.Net MVC 5
 
3. Now press F5 to run the application and you should see a message displayed on page in browser.
Getting Started with Razor View Engine in ASP.Net MVC 5
 
 
Downloads