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.
ASP.NET MVC was designed for creating websites. It is a web application development framework just like web pages.
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.
- 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.
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 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
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
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.
We should create our website in MVC because of the following reasons: