Creating Your First ASP.Net MVC Application

This article explains how to create an ASP.NET MVC application using Visual Studio. We create an application that shows "Hello World!" on the UI. If you want to learn what MVC is go through this link:

ASP.NET MVC Introduction 

Now we go step-by-step to create an ASP.NET MVC application.

1. Open Visual Studio.

Click on "New Project" in the Start Page:

MVC-application-1.jpg
or

Go to "File" -> "New" -> "Project...".

MVC-application-2.jpg 
We get the installed templates pane.

2. Click on Web, select "ASP.NET MVC 2 Empty Web Appliction" from the right pane. Now provide an appropriate name of the application in the Name field at the bottom and provide a location for the application.

MVC-application-3.jpg

3. After completing step 2 we get an ASP.Net MVC application skeleton that has the folder structure (empty folder), static files or publically servable files. Here we have default *.css and *.js files and empty folders consistent with the MVC architecture. 

MVC-application-4.jpg

 To learn more about the folder structure above, go through this article:
 
Folder Structure of ASP.NET MVC Project

4. Add controller

Right-click on the "Controllers" folder in Solution Explorer and go to the "Add"  item then click on "Controller".

MVC-application-5.jpg 
Now we get the following screen.

MVC-application-6.jpg 
Provide the Controller Name. I used the name Home and Controller is a suffix because each controller has it in its named. So the Controller name is HomeController. Leave the checkbox unchecked because we don't want to create these actions in HomeController. Our HomeController has an Index action to get a command and send the view on the Index UI.

using System.Web.Mvc;

 

namespace MvcApplication.Controllers

{

    public class HomeController : Controller

    {

        public ActionResult Index()

        {

            return View();

        }

    }

}
 
We want to show a message "Hello World!!" on the UI so we modify our action result Index(), in other words we put the message in ViewData so that it can be accessed on the associate view of this action.

 

public ActionResult Index()

{

    ViewData["message"] = "Hello World!!!";

    return View();

}

5. Add View

Right-click on ActionResult Index() in HomeController class then click on  Add view.

MVC-application-7.jpg
 
We get the following window. We have ActionResult Index() so the View name will be Index and we can't change it and uncheck the check box Select Master page because we don't have a master page. Then click on the Add button.

MVC-application-8.jpg

We have HomeController and ActionResult Index() in it so after clicking on the Add Button, the Home  folder is created under the Views folder that contains Index.aspx as the view.

MVC-application-9.jpg

We want to show the message "Hello World!!" on the UI so we call the ViewData message on the view to show it.

<%= ViewData["message"] %>
 
Now our view looks like:

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

    <title>Index</title>

</head>

<body>

   <%= ViewData["message"] %>

</body>

</html>
 
Now press F5 to run the application or click on the Start debugging button in Visual Studio toolbar and get the result on the UI.


MVC-application-10.jpg


It is a simple MVC example that explains the basics of MVC application creation. It works as in the following diagram.


MVC-application-11.jpg

Up Next
    Ebook Download
    View all
    Learn
    View all