Click here to Skip to main content
Click here to Skip to main content

Tagged as

MVC Introduction

, 22 Oct 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
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.

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.

  • 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 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.

MVC Features

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

License

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

Share

About the Author

Shivangi Bhatia
Software Developer Morpho E Documents
India India
No Biography provided

Comments and Discussions

 
GeneralMVC Architecture image Pinmemberacarpio19752-Apr-14 15:50 
GeneralGreat Article! PinmemberNeil Diffenbaugh19-Nov-13 7:00 
SuggestionIncorrect Explanation PinmemberDavid Rogers Dev16-Nov-13 20:25 
SuggestionImage in MCV Request Life Cycle. Pinmember VICK22-Oct-13 21:44 
GeneralMy vote of 4 Pinmember VICK22-Oct-13 21:43 
QuestionThe image in ASP.NET MVC Request Life Cycle topic is very small. PinmemberWilliam John Adam Trindade22-Oct-13 12:52 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141223.1 | Last Updated 22 Oct 2013
Article Copyright 2013 by Shivangi Bhatia
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid