Click here to Skip to main content
15,861,168 members
Articles / Web Development / ASP.NET
Tip/Trick

MVC Introduction

Rate me:
Please Sign up or sign in to vote.
4.44/5 (27 votes)
22 Oct 2013CPOL3 min read 54.7K   4   32   6
A brief introduction to ASP.NET MVC

MVC Introduction

In this tip, we will cover an introduction to ASP.NET MVC. But before that, we will have a look at the release history of ASP.NET MVC.

Image 1

ASP.NET MVC was designed for creating websites. It is a web application development framework just like web pages.

MVC Architecture

MVC does not replace Web Forms. We can create our web application by using the MVC framework or ASP.NET Web Forms. MVC is just an alternative architecture to the standard ASP.NET Web Forms model. This design pattern separates our website into three layers which increases the control on our web application.

Image 2

  • Model – Model represents business logic and data. It contains the properties and application logic. It communicates with the database, i.e., retrieves data and stores data in the database. It can be LINQ to SQL or Entity Framework.
  • View – View represents the presentation layer of the application. It is responsible for providing the User Interface (UI) to the user. Basically, it is a set of web pages (ASPX) or User Controls. There is no input logic or processing of user actions, these are managed inside the controller. This separation makes the application more testable.
  • Controller – HTTP requests are routed to individual controllers and then the controller calls the model and selects a View for displaying the results. The controller acts as a coordinator between the Model and the View.

It is a loosely coupled development framework as it is divided into three layers. This loose coupling helps in reducing the complexity of the web application, and makes it is easy to maintain and provides better Test Driven Development which was one of the goals of ASP.NET MVC – to increase the testability of applications.

An MVC application has different architecture, structure, and page processing than a web form. In an ASP.NET application, requests are handled by ASP.NET; i.e., ASP.NET calls the page, executes the events, and then returns the response. But in MVC, we have to write the models, create the views and code the controllers. In MVC, requests are handled by UrlRoutingModule HttpModule. This module parses the request, selects a route (we will talk about routing later) based on the configuration which we will provide. Then request is routed to one of the controllers that we write. Then, it is the controller’s responsibility to access the database through Models and provide the response to UI through Views. Thus, we have our control on requests and response. So, in MVC, nothing is hidden from us. We have to code more in MVC as there is nothing automatic.

Image 3

ASP.NET MVC Request Life Cycle

MVC uses routing for processing for which we have to use System.Web.Routing namespace. As we have already discussed, MVC requests are handled by URLRoutingModule HTTPModule. URLRoutingModule takes the request and looks up for a route in Route Collection table. We add routes to this collection inside Global.asax file under RegisterRoutes method which is called by Application_Start event. These routes get added to the table when the application starts. The request is then mapped to a route and two objects are created named – RouteData to represent the Route and RequestContext to represent the request.

MVCRouteHandler class handles the routing. It creates an instance of MVCHandler and passes the RequestContext to the handler. That handler actually calls the controller (the controller class that we write to handle the processing of requests) and run its Execute method.

The controller then uses ControllerActionInvoker to determine which action (in Controllers, we use Action to execute the code that acts as events) to run. Then the Action method accepts the user input and prepares a response by using the View and Model.

Image 4

MVC Features

We should create our website in MVC because of the following reasons:

Image 5

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Morpho E Documents
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMVC Architecture image Pin
acarpio19752-Apr-14 14:50
acarpio19752-Apr-14 14:50 
GeneralGreat Article! Pin
Neil Diffenbaugh19-Nov-13 6:00
Neil Diffenbaugh19-Nov-13 6:00 
SuggestionIncorrect Explanation Pin
David Rogers Dev16-Nov-13 19:25
David Rogers Dev16-Nov-13 19:25 
SuggestionImage in MCV Request Life Cycle. Pin
VICK22-Oct-13 20:44
professional VICK22-Oct-13 20:44 
GeneralMy vote of 4 Pin
VICK22-Oct-13 20:43
professional VICK22-Oct-13 20:43 
QuestionThe image in ASP.NET MVC Request Life Cycle topic is very small. Pin
William John Adam Trindade22-Oct-13 11:52
William John Adam Trindade22-Oct-13 11:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.