ASP.NET MVC First Application Step By Step

Here we will learn to create a demo application with Empty Project Template. In the first article, we learned an overview on ASP.NET MVC.

Pre-requisite for MVC

Before starting, there are lots of versions, such as MVC 2, MVC 3, MVC 4, MVC 5 and MVC 6. So, it depends to you which version you are using to integrate with Visual Studio.

If you are working with MVC 5, it needs VS 2013. We can download it from the following link,

If you are working with MVC 3 and 4 you need VS 2012.

If you are working with MVC 2, then you need Visual Studio 2010 or 2008.

Create New Project

To create new project, Open Visual Studio, I am using Visual Studio 2013 and click on File Menu, then New and choose Project.

New project

It will open a new window from where you can choose the application type. Choose Web from Installed and then choose ASP.NET Web Application. Give it proper project name and also provide the location of project to save and click on OK.

first MVC Project

This will open a dialog where we can choose New ASP.NET Project. So, here we are going to choose an Empty Project, and actually here we will learn everything from beginning so an empty application is best. From project type, we need to use MVC and choose OK.

use MVC

So, finally we have created an ASP.NET MVC empty application. When you go through the Solution Explorer, project structure will look like the following. Here we can see different types of folders and files, such as Controllers, Models, but these are empty.

MVC application
Adding Controller

As we all know Controller is responsible for handling the request which is coming from the user interface [ex: View]. So, first we are going to add a new controller and corresponding we will also add a View.

To add new controller, Right click on Controllers folder and choose Add and then click on Controller.

Add New controller

It will open Add Scaffold window, where we can choose different types of controller. Here we are interested to add simple mvc controller. So, choose MVC 5 Controller – Empty and click on Add.

MVC 5 Controller

After clicking Add, it will open a Add Controller window, here you need to pass the name of controller and choose Add.

Add Controller

So, finally we have added a HomeController, the sample code of HomeController will be like the following screenshot:.

HomeController

Adding View

So, it’s time to create a user interface, So, we can add a view where we will display the data. Actually, View is used to show the data which is coming from the controller. It is basically a GUI of ASP.NET MVC application.

To add a view, Right click on Action Name or View() and choose Add View.

Add View

It will open an Add View dialog where we can choose the name of view, type of template. Also we can choose here layout page for the view. It will auto take the name of View as per Action Name. Click OK.

Index

When we click on Ok button, It will create a Home folder inside the Views directory and inside the Home directory, it will create an "index.cshtml" page as view.

When we created the project, there was not any layout page added into the project, but after adding a view, it will auto create the _Layout.cshtml page for layout.

We can make some changes in Views as in the following.

View

So, finally everything is ready for basic ASP.NET MVC application. It's time to run the project, so press F5.

run the project

When we open this application in small screen size, it auto convert as responsive application. It is because, it is using bootstrap.

responsive application

Thanks for reading this article, hope you enjoyed it.


Similar Articles