Creating First ASP.NET MVC Application

 Let us go ahead and see how to create and run a very basic MVC Application.

Step 1: Go to Visual Studio, FILE, New, then click Project. It will open a popup.



Step 2: Select Web from the left hand pane. On the right hand side Select ASP.NET MVC 4 Web Application.



Step 3: Select Empty template as we do not want pre generated code and class files as of now. We will be using Razor Engine so it is auto selected and need not be changed. Click OK.



Visual Studio creates the following folders under MVC project. Let us see the contents generated by the Visual Studio in the application.



We get three folders named Model, View and controller. Since in MVC the user interacts with a controller so let us now add a controller to the application. Right Click on Controllers folder-> Go to Add-> Select Controllers.



The following popup appears next. We will name our controller here. Name it “Home”. Controller keyword is auto appended. So it is recommended not to remove it from the textbox. We would be selecting Empty MVC controller as the template.



Following is the content in the controller we just created.

  1. using System;  
  2. usingSystem.Collections.Generic;  
  3. usingSystem.Linq;  
  4. usingSystem.Web;  
  5. usingSystem.Web.Mvc;  
  6. namespaceMyFirstMVCApp.Controllers  
  7. {  
  8.     public class HomeController: Controller  
  9.     {  
  10.         //  
  11.         // GET: /Home/  
  12.         publicActionResult Index()  
  13.         {  
  14.             return View();  
  15.         }  
  16.     }  
  17. }  
Let us write our own method in the controller.
  1. publicActionResult Display()  
  2. {  
  3.    return View();  
  4. }  
It will return a view. So let us add a view to display some content in the view.

Right click on Display method-> Go to Add View at the top. Click on it.



The following popup appears for entering the name of the view. The name shown here is the same name as that of the ActionResult. It is auto populated and needs to remain the same. We will use Razor Engine and since there are no models as of now to deal with so we will keep the checkbox unselected. Hit Add.



The following is the text generated in the View.



When we create a view for a particular Actionresult a folder is created in the View folder corresponding to the ActionResult method. Let us check whether this folder is created or not.



Let us now change the display message in the view to something like.
  1. @{  
  2.    ViewBag.Title = "Display";  
  3. }  
  4.   
  5. <h2>This is my First MVC Application!!</h2>  
Let us now run the application and check what is displayed.



Oops we get an error instead of some meaningful message. Well this is because the default route specified in the route config is something else. We can find the route config file under App_Start folder.



The route specified in Rout config under App_Start folder is:
  1. routes.MapRoute(  
  2.    name: "Default",  
  3.    url: "{controller}/{action}/{id}",  
  4.    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }  
  5. );  
It accepts Home controller and Index Action. But we have Display ActionResult in our code so we will modify our Url to.

Localhost:1031/Home/Display

This is because when we run our MVC application the default route runs or it is prioritized. We can always define our own custom routes. Let us change the URL and run the application now.



So we just created our first MVC application. In the further posts we will be studying the use of models , views and controllers in detail.

 

Next Recommended Readings