Introduction
This session is the second part of the series, where you will learn about ASP.NET MVC step by step for beginners. I suggest reading Part One of the blog.
Now, it’s time to get our hands dirty by creating our first ASP.NET MVC Application. We will create a new MVC 4 Application with Visual Studio 2012 for the Web and understand the basic building blocks of a MVC Application.
Create our first simple MVC Application
Step 1
First of all, open Visual Studio 2012.
- File-> New-> Project.
- Visual C#-> Web-> ASP.NET MVC 4 Web Application-> Name your project -> OK.
- From the New ASP.NET Project dialog, select MVC, as shown below. Also, select View Engine: Razor.
- Visual Studio creates the folder structure given below for MVC Applications, by default.
Step 2
Adding New Controller
- In Visual Studio, right click on the Controller folder -> select Add -> click Controller.
- It will open Add Controller dialog, as shown below.
- Write Controller name. Do not delete the word Controller. Remember, controller name must end with Controller.
- This will create HomeController class with Index method in HomeController.cs file under Controllers folder, as shown below.
Controller
- Handle Requests to the application by the user.
- Get the data from the database.
- Specify a view that returns a response to the client.
Step 3
Add View
- Open a HomeController class -> right click inside Index method -> click Add View.
- In Add View dialogue box, keep the view name Index. It's good practice to keep the view name the same as the action method name, so that you don't have to specify view name explicitly in the action method, while returning the view.
- Select View Engine: Razor.
- The code is given below.
View
View is a user interface. View displays data from the model to the user and also enables them to modify the data.
Step 4 Adding Model
- Right click on Model folder -> Add -> click class.
- In the Add New Item dialog box, enter the class name 'MyFirstModel' and click Add.
- This will add new MyFirstModel class in model folder.
Model
- Represents the Data of the Application.
- It maintains the data of the Application.
- Model objects retrieves and stores model state in a database.
In this way, you can create your first MVC 4 Application, using Visual Studio 2012.
ASP.NET MVC Folder Structure
Visual Studio creates the folder structure given below for MVC Application, by default.
Let's see the significance of each folder
- App_Data
App_Data folder can contain application data files like LocalDB, .MDF files, XML files and other data related files.
- App_Start
App_Start folder can contain the class files, which will be executed when the application starts. Typically, these would be config files like FilterConfig.cs, RouteConfig.cs etc.
Global.asax
Global.asax allows you to write the code that runs in response to Application level events, such as Application_Begin Request, application_start, application_error, session_start, session_end etc.
Packages.config
Packages.config file is managed by NuGet to keep the track of what packages and versions you have installed in the Application.
Web.config
Web.config file contains the Application level configurations.
Creating a simple hello world ASP.NET MVC Application.
See this example here
So far, we discussed MVC architecture in ASP.NET, including the flow of the user's request in ASP.NET MVC, Routing Engine in ASP.NET MVC, creating a first simple MVC Application, ASP.NET MVC folder structure, and creating a simple hello world ASP.NET MVC application.
We are going to discuss about data validation in MVC in the next part.