Introduction

This post gives you the basic overview on ASP.NET Models, Views and Controllers. It explains how all parts in MVC Application work together and discuss how the Architecture of an ASP.NET MVC application differs from an ASP.NET Web Forms application.

The Sample ASP.NET MVC Application
  1. Launch Visual Studio 2008, Select the menu option File, New Project then New Project Dialogue box appear as shown below

    mvc1.gif

    Select the ASP.NET MVC Web Application Template from the dialogue box and click Ok.

  2. When you create a ASP.NET MVC Application, Create Unit Test Dialogue box appears as shown below.

    mvc2.gif

    Select the No, do not create a unit test project and click Ok.

  3. After ASP.NET MVC Application is created. You will see several folders in Solution Explorer. You will find three folders named Models, Views and Controllers.

    mvc3.gif

    If you expand the Controllers Folder, you will see a file named HomeController.cs and if you expand the Views folder you will see About.aspx and Index.aspx under Home Sub Folder.

  4. Now you run the application you will see the following output

    mvc4.gif
Notice the URL in the Address bar, When you click the Home menu link, The URL in the browser changes to /Home and when you click the About menu link, the URL changes to /About.

If you return to Visual Studio project you do not find the Home or About page.

A URL Does not Equal to a page in the application URL Routing Controllers

A Controller is responsible for sending the response back to a user who makes the request. Controller is just a C# class file. The Sample MVC Application contains the controller named HomeController.cs located in the Controllers folder.

The Controller in the ASP.NET MVC Application looks like

mvc6.gif

Note: The two methods in the controller Index() and About() corresponds to two actions Home and About clicks.

Views The HomeController.Index() action returns a view located in the following path

\Views\Home\Index.aspx

If you want to return a view for a controller action, you need to create a sub folder in the Views folder same name as controller and create a .aspx file with same name as the action.

Models

A Model in ASP.NET MVC Application contains logic that is not in the view or a controller. The Model should contain your business logic and Data access logic.

Conclusion

We had a overview on ASP.NET MVC application and URL Routing. We learnt the functionality of Model, Controller and View in the ASP.NET MVC Application.