Your First Asp.Net MVC Application

From my previous posts you can learn what are the advantages of Asp.Net MVC and how to install Asp.Net MVC.

In this post I am explaining how to create simple Asp.Net MVC application.

Open Visual studio 2008 Framework.  Go to File -> New -> Project.

From the left side panel select Visual C# as language and from right side select Asp.Net MVC as shown below.

 

 

This is your first Asp.Net MVC application, I am giving the name also as FirstMVCApplication. Then click OK.

When you click OK, the first thing you’ll see is a pop-up window asking if you’d like to create a unit test project. This is your first Asp.Net MVC application, so we are not writing any unit test for our application. Choose "No, do not create any unit test project".

Visual studio creates the default project for you. Press F5, then Browser will displays the simple page with header like My MVC Application.

Now you have to create your own application by writing your own code. Before writing your first Asp.Net MVC application you have to delete some files from your project. Why because Visual studio already created a default application for you. To create your own Asp.Net MVC application, you have to delete some unnecessary files from your project.

To delete unnecessary files, Open Solution Explorer and select the files mention below.

App_Data folder, AccountController.cs controller from Controllers Folder, Account  folder from Views folder ,  About.aspx and  Index pages from Home folder in the Views folder and all files from Shared folder in the Views folder.


You can delete above files individually by right click on each file and select Delete.

Now your project is ready to create your own Asp.Net MVC application.


Open HomeController.cs controller from Controller folder and delete all code inside the HomeController class and place your own code shown below.

public class HomeController : Controller
    {
        public string Index()
        {
            return "Hi, This your First Asp.Net MVC application.";
        }
    }

Now execute your applicatin by pressing F5, then the message displays on Browser what you wrote in Index method.

 

So, you successfully wrote the your first Asp.Net MVC application. But you have to learn how Asp.Net MVC work?. In MVC(Model View Controller) arichitecture Controllers are responsible for handling incoming requests. Here Controllers are just C# classes derived from System.Web.MVC.Controller.Each controller has it’s own public methods and each public method in the controller is known as action method because you can invoke each method from Browser via URL. Here HomeController.cs is a Controller and Index() is the action method in this controller. So when you send the request from browser by using http://URLOfYourSite or http://URLOfYourSite/Home or http://URLOfYourSite/Home/Index, it get back the output from the HomeController Index method.

 

You successfully created the your first Asp.Net MVC application by rendering the simple text. Here we didn't use the any HTML tags to display our text. But in real world applications we have to use HTML format only to display content on web pages.

 

Download Source code for the above article at FirstMVCApplication.zip (254.43 kb)

 

In my next post I will explain how to render HTML controls in Asp.Net MVC.