How ASP.NET MVC Architecture works?

+1 vote
asked Jun 18, 2013 in ASP.Net MVC by anonymous

Can someone help me understanding Asp.NET MVC architecture? How does it work internally? How the communication happen between the web browser and webserver. How the requests get processed in MVC framework and how the response sent back to web browser?

Share

1 Answer

+1 vote
answered Jun 20, 2013 by administrator (315 points)
selected Jun 21, 2013 by Aadhira
 
Best answer

ASP.NET MVC requests are processed totally different than regular ASP.NET where the URL will have direct mapping with a file in the server. For example, if you have an URL like

http://www.example.com/examplepage.aspx

The above URL is mapped with the file called examplepage.aspx. If the file is not there, then the URL is invalid. But in MVC, the URL is not mapped with any particular file; instead it is mapped with the route. ASP.NET Routing will help us to find the right resource based on the URL we requested.

Ex: http://www.example.com/examplebag/pageid

 

Here are the steps involved when we request an URL in a web browser for a web page which is developed using ASP.NET MVC.

Asp.Net mvc architecture overview

 

1 When the URL is requested in the web browser, the request will be sent to the web server (IIS) where the web site is deployed.

2. Once the request reached IIS then the IIS will send the request to the respective application. In our example, it is going to “example.com”

3. Even if the IIS is running more than one website, each website will be running in their own app domain.  So, “example.com” will be running in its own AppDomain.

4. Once the request comes to the AppDomain, it will reach the httpruntime. This http runtime will create the find the HttpApplication (most of the time global.asax) using HttpApplicationFactory. This is the entry point of ASP.NET web application, this will have a class that is derived from System.Web.HttpApplication.

5. HttpApplication will be using UrlRountingModule (This is an IHttpModule) to find the right routing configuration. Since the UrlRoutingModule is an IHttpModule, this will executed for each and every request that comes for this web site. So, whenever a request comes, it will come to the UrlRoutingModule first.

6. Then, UrlRoutingModule will find correct first matched routing configuration from all the routes configured. In ASP.NET MVC, this configuration will be done through global.asax using MapRoute method (actually an extension method) in RouteCollection. This RouteCollection object can be accessed using static property of RouteTable with the property “Routes”.

7. Using the configuration based on step 6, UrlRouting will find the first matched route (If you have configured multiple route pattern, then it will pick the first matched route from the give order), and creates RequestContext using the RouteDate.

8. UrlRoutingModule will now create an instance of another class called MvcRouteHandler and pass the “RequestContext” which was created in Step 7 to the MvcRouteHandler.

9. Now, MvcRouteHandler will create instance of another class called MvcHandler and pass the “RequestContext” to that object. This MvcHandler is of type IHttpHandler.

10. Next, MvcHandler will use a factory class (DefaultControllerFactory) which is derived from IControllerFactory, to create instance of the Controller which is created by the developer. DefaultControllerFactory will look into our assembly and find all classes which are derived from “IController” also which has the same name as the Controller Name that came from the Url(This information will be available in RequestContext object).

In our example, controller name is “examplebag”. So, the “DefaultControllerFactory” will find any class with the name “examplebagcontroller” (routes are case insensitive) also, “examplebagcontroller” must have derived from IController.

11. Once the respective controller identified and the instance created, then MvcHandler will call the IController method “Execute”. Now you controller code will get executed.

12. Controller then talks to Model and the respective view and generate the html with proper date and send the response back to the browser.

If you look at the flow carefully, the request is first reaching the controller class, not the .aspx. And there is no code behind file require for any aspx page in mvc.

Your answer

Preview

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.
site design / logo / content © 2013 - 2015 pinfaq.com
...