Create First ASP.Net MVC Application

In this article we will learn how to create a MVC project.
 
ASP.Net MVC is basically an architectural pattern that allows us to create a dynamic website using HTML. This gives the feature of "TDD development".
 
The following is the procedure to create your first ASP.Net MVC project.
  • Open Visual Studio and click on "File" -> "New" -> "Project...".
  • You will see a screen. From here select Web and ASP.NET MVC4 Web Application and provide the name for this project and click OK. 
 
When you click OK we will see an ASP.Net MVC New Project screen. From here select “Empty template “ and select Razor as ViewEngine. Here Razor is two types, one is ASPX and the other type is ViewEngine.

Here we will prefer “VIEWeNGINE” because it is much cleaner and better to use.

 
After clicking on OK, it will create a MVC project. In Solution Explorer we will see “Modal”, “view” , “controller” folder.
 
Now go to the Controller folder and add a new Controller to the project .

Right-click on the Controllers folder then select Add -> Controller.

 
Provide the name for this controller the As FirstController and click on Add and it will generate a file with the name FirstController.


Here when you will click on add this will add a class “FirstController.cs” class. It will be available in the Controller folder. This class will be inherited by the “Controller” base class.
 
Here this class will contain a method and the return type of this method is “ActionResult”.

Add ActionMethod To the Controller class

ActionMethods are used for handling “user request”, validation of user input, execute code and the o/p.
 
The MVC routing engine sendsthe user request to the appropriate ActionMethod.

Now open the “FirstController.cs” class and add anActionMethod and the provide name as “Index”.
 
The following code is for the FirstController class.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. namespace FirstMvcApplication.Controllers   
  7. {  
  8.     public class FirstController: Controller   
  9.     {  
  10.         //  
  11.         // GET: /Home/  
  12.         public string Index()   
  13.         {  
  14.             return "Hello, from first MVC application";  
  15.         }  
  16.     }  
  17. }
There is a function “index” that will return a String. Now at this point if you run this application it will deploy to the local server and will provide the output.
 
Here in this condition if you run this application it will provide error. It is because if you go to the App_Start -> RouteConfig class there is code that contains the first method and which the Controller class will execute.

 
Here by default the Controller name is “Home” but we don't have a Controller with this name. So for execution this changes the name of the controller from Home to First.

 
Change this Home Controller to First controller For this the code is below.
  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. namespace FirstMvcApplication   
  8. {  
  9.     public class RouteConfig   
  10.     {  
  11.         public static void RegisterRoutes(RouteCollection routes) {  
  12.             routes.IgnoreRoute("{resource}.axd/{*pathInfo}");  
  13.             routes.MapRoute(  
  14.             name: "Default",  
  15.             url: "{controller}/{action}/{id}",  
  16.             defaults: new {  
  17.                 controller = "First", action = "Index", id = UrlParameter.Optional  
  18.             });  
  19.         }  
  20.     }  
  21. }  
Now run your application and the output will be "Hello, from first MVC application".

At this time this application is running on the local sever. For deploying this application to the IIS Sever there use the procedure:
 
Go to Start the press Run then in the Run window type “InetManager”.
 
Or

Go to Control Panel then select Administrative Tools then search for Internet Information Services (IIS) Manager.

Here if you are not able to see Internet Information Services (IIS) Manager then install it in your system. For this go to How To install IIS Server.

On the IIS server screen you are not able to see your application (FirstMVCApplication) so for this go back to you application and right-click on your application and go to properties and click on WEB. You will see there that you are using the Visual Studio development server but instead of that we want to use the local IIS web server so for this check the radio button.
 
Remember that for creating you application on IIS server you need to open VS2010 as “Run as Administrator”.
 
Now click on the Create virtual directory.

 
After creating the virtual directory again go to IIS Server then refresh it and then you will see your application there. It means now your application will run on the IIS server.

 
Now when you run this application you will see the URL it is running on, “localHost/FirstMVCApplication/”.

You will see the output “
"Hello, from first MVC application"”.

Up Next
    Ebook Download
    View all
    Learn
    View all