Creating First Application In ASP.NET MVC 4.0

In this post, we will learn how to create first application in ASP.Net MVC 4.0. For demonstration, I am using Visual Studio 2012 which include MVC 3 as well as MVC 4. If you don’t have ASP.NET MVC 4 installed on your system, then go to the following MVC 4 link and click on install ASP.NET MVC for free.



If you don’t know which version of ASP.NET MVC is already installed in your system, then you can find it by visiting the Control Panel, Programs and Features.



What is MVC?

MVC is a framework for building web application using the following three architectural component pattern i.e.
M: Model V: View C: Controller:
  • Model is responsible for data access and known as Business Layer.
  • View contains the User Interface (U.I.) of the Application.
  • Controller is the part of application which handles user request. As the name suggest, Controller controls the entire application. 
Graphical Representation of MVC (User Request Flow):


Create a New ASP.Net MVC Project:

1. Go to File, New, then Project.

2. Under Web Template select ASP.Net MVC 4 Web Application and give it a meaningful name.

And then click OK. After that another dialog box opens.

3. Select Empty Template and View engine as Razor.

 

Selecting MVC Project as Empty creates basic folder structure for the project. You will also see two View Engine ASPX and Razor. Razor view engine is new and improved view engine introduced in ASP.NET MVC 3. In Solution Explorer, you will see the following three main architecture components of ASP.NET MVC project that is Model, View and Controller.

 

If you try to run your Application, you will get HTTP 404 Error because we have created ASP.NET Empty Web Application and our application contains nothing/no U.I. to display.



Let’s Add a Controller in our MVC project. All of the request (by user) is handled by Controller. Right Click on Controller Folder and then Go to Add, then Click on Controller.

 

In Add Controller Dialog, Add Controller name as Home. By Convention, Every controller name ends with Controller Word so, our Controller name becomes HomeController. Never try to Remove Controller word from your Controller Name. In Scaffolding options, select Empty MVC controller template. This is our first application and we try to keep it as simple as possible.



After Adding the Home Controller you will see HomeController with an Index method.

 
Make a small change in Index Action method. Change the return type of Index method to string and return a string i.e. "Hello World Application using ASP.NET MVC 4". We will learn about View and ActionResult in our next post.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6.   
  7. namespace FirstMVCApplication.Controllers  
  8. {  
  9.     public class HomeController : Controller  
  10.     {  
  11.   
  12.         public String Index()  
  13.         {  
  14.             return "Hello World Application using ASP.NET MVC 4";  
  15.         }  
  16.   
  17.     }  
  18. }   
Now Run the Application and you will see the following result.

 

If we type http://localhost:28473/home/index and press enter we will get the same result. 

 

By Default when we run ASP.NET MVC project, the request goes to Index Action method of HomeController. We can also specify which controller or Action method runs, on running the project. All of these settings for URL mapping are present in RouteConfig.cs file present App_Start Directory.

 

In RouteConfig.cs, you will see url: "{controller}/{action}/{id}" means firstly we have to pass controller name and then action name separated by forward slash (/). We can also pass parameter which is Optional. Here you will see default controller specified as HomeController and default Action as Index.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. using System.Web.Routing;  
  7.   
  8. namespace FirstMVCApplication  
  9. {  
  10.     public class RouteConfig  
  11.     {  
  12.         public static void RegisterRoutes(RouteCollection routes)  
  13.         {  
  14.             routes.IgnoreRoute("{resource}.axd/{*pathInfo}");  
  15.   
  16.             routes.MapRoute(  
  17.                 name: "Default",  
  18.                 url: "{controller}/{action}/{id}",  
  19.                 defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }  
  20.             );  
  21.         }  
  22.     }  
  23. }  
Let’s add another action method in HomeController.
  1. public class HomeController : Controller  
  2. {   
  3.     public String Index()  
  4.     {  
  5.         return "Hello World Application using ASP.NET MVC 4";  
  6.     }  
  7.     public String About()  
  8.     {  
  9.         return "This is our About us Page";  
  10.     }  
  11. }  
And we want to call About() action method of HomeController by default on running the application. So go to App_Start Directory, then click on RouteConfig.cs file. In routes.MapRoute method change the action to About. Now Build and run the project.

RouteConfig.cs Code:
  1. routes.MapRoute(  
  2.                 name: "Default",  
  3.                 url: "{controller}/{action}/{id}",  
  4.                 defaults: new { controller = "Home", action = "About", id = UrlParameter.Optional }  
  5.             );  
 

I hope you enjoyed this post. Thanks! 


Similar Articles