Sunday, 22 January 2017

Creating your first ASP.Net MVC 4 application

In this article, I am going to teach you how to create simple ASP.Net MVC 4 application from scratch. I am using visual studio 2012 ultimate. Below is the step by step tutorial.

Creating ASP.Net MVC 4 application

Step 1

Go to File-> New-> Project.. as shown below.

Creating your first mvc 4 application

Step 2

The new window will open as shown below. Go to Web under Installed Templates, select ASP.Net MVC 4 Web Application. Give your project's meaningful name and click on OK.

Creating new project

Step 3

The new window will open as shown below. Select Empty from Project Template. Select Razor View engine and click on OK. I will explain you more about View engines in later articles.

Select razor view engine

After that a new project will be created as shown below. Visual studio will create necessary folder that is required to run MVC application. Controllers, Models and Views are necessary folder get created. Initially all the folders will be blank. mvc folder structure

Adding Controller

Step 4

Now the next important step is to add controller. Right click on Controller folder, go to Add and click on Controller..

Adding controllers

New window will open as show below. Give controller's meaningful name. Do not remove Controller keyword from Controller Name. Select Empty MVC controller from scaffolding options and click on Add. I will explain you scaffolding in later articles.

Give controller name

Visual studio will generate a class HomeController which inherits from Controller class. Source code generated is shown below.

namespace MvcApplication1.Controllers
    public class HomeController : Controller
        // GET: /Home/
        public ActionResult Index()
            return View();

Passing data from Controller to View

Step 5

Now we will pass data from controller to view using ViewData. Give key name to ViewData as ["Data"] and assign some string.

namespace MvcApplication1.Controllers
    public class HomeController : Controller
        // GET: /Home/
        public ActionResult Index()
            ViewData["Data"] = "Welcome to ASP.Net MVC";
            return View();

Adding View

Step 6

Next step is to create View. Right click within Index() method, click on Add View.. as shown below.

Adding view

New window will open as shown below. Give View name as Index as shown below.

Adding view with razor engine

Now a new folder Home will be created within Views folder and a new page with name Index.cshtml will be created as shown below.

Views folder in mvc

Step 7

Now open Index.cshtml file. Use @ symbol to access ViewData as shown below.

    Layout = null;
<!DOCTYPE html>
    <meta name="viewport" content="width=device-width" />

Now the next step is to build and run the application. Output will be shown as below

Running mvc application

Go to App_Start folder and open RouteConfig.cs file. As you can see RouteCollection contains name and url. In our application Default route will be invoked. Here the controller name is Home and action method name is Index. I will explain more about routing MVC application in later articles.

Routconfig file

In the browser, you can hit home/index where home is the controller name and index is the action method.

Controller and action method


Post a Comment

Email Subscription

Subscribe to our newsletter to get the latest articles directly into your inbox

Blog Archive