Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

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.

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.

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.

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.

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..

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.

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.

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

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.

Step 7

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

@{
Layout = null;
}
!DOCTYPE html>
html>
head>
meta name="viewport" content="width=device-width" />
title>Index/title>
/head>
body>
div>
@ViewData["Data"]
/div>
/body>
/html>

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

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.

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



This post first appeared on ASPArticles, please read the originial post: here

Share the post

Creating your first ASP.Net MVC 4 application

×

Subscribe to Asparticles

Get updates delivered right to your inbox!

Thank you for your subscription

×