How MVC and IIS work

Source: Internet
Author: User

Learn how IIS & MVC works

I have been puzzled by a request from the client requesting how the server side is connected to IIS, and how IIS reads my published code and returns the files on the server. This is how a process of processing.

1: When you enter an address from the browser or click on a link to start, you have sent an HTTP request (will be based on the requestor request the host header or IP or port number to find the corresponding site);

2: According to the HTTP protocol, when the request arrives at the appropriate host server, the system process on the server, HTTP. SYS (which can be understood as a process that specializes in processing the request) is received;

3:http.sys the worker process passed to the application pool after receiving the request signal, the IIS process inetinfo.exe, Note: The server process is connected to IIS at this time.

The IIS process can handle general static pages such as. html, and then return the found HTML page directly to the client display after processing;

4: If the. aspx or cshtml page is not processed directly by IIS, then the IIS process will load a call aspnet_isapi.dll;

5: When the ISAPI receives a processing request, an ASP. NET worker process is started, and then the request information is forwarded to the ASP. NET worker process (aspnet_wp. EXE), Note: This is when IIS is connected to ASP . Next, control is mastered by aspnet_wp.

"Let's get to know what the AppDomain is, feel this good http://allanpie.blog.163.com/blog/static/21320410200921311273632/, I understand that the AppDomain is an environment or application domain that can handle the ASPNET process (. dll), and we know that the operation of the ASP. NET program needs to be based on the. Asp. NET is a complex engine that uses managed code to process Web requests from beginning to end.

There are several important objects in the AppDomain.

Isapiruntime: It is specifically responsible for extracting the necessary information from the HTTP request and forwarding the information and requests to httpruntime.

HttpRunTime: The main job is to set up a HttpContext object for every customer who requests it. This stuff manages the HttpSession object.

Each visitor has its own HttpContext object and HttpSession object, and you can find the corresponding class name in the. NET Framework library, like System.web.httpcontext,system.web.

HttpSessionState and so on. These two objects are part of the AppDomain runtime environment. All of these two classes are called System.web.hosting.isapiruntime,system.web.httpruntime. 】

6: When control is handed over to the aspnet_wp process, if no. NET environment is executed, the AppDomain is first established to execute the application domain, and when the AppDomain is established, The two classes Isapiruntime and httpruntime are instantiated as part of the AppDomain.

7: When the AppDomain initialization is complete, the next step is to establish a session, pass the request to one of the classes httpruntime, and create a relationship between them: httpruntime is responsible for creating HttpContext and HttpSession,

HttpContext is responsible for managing httpsession.

From the time the HTTP request is submitted to this (httpruntime created HttpContext), in fact, the application we publish on the server is still not running, or the requestor's request is actually not really handled, The work ahead is a bit of preparatory or ancillary work.

8: Next, HttpRuntime creates an important object HttpApplication in addition to creating HttpContext and HttpSession.

HttpApplication calls the ProcessRequest method to handle a user request, specifically creating a HttpContext instance,

9:httpruntime uses contextual information to find or create a new object that can handle the request's Web application. The HttpApplication factory is responsible for returning the HttpApplication instance. The HttpApplication object uses an instance of the IHttpHandlerFactory type to return a HttpHandler (HTTP handler) to the HttpRuntime object. A page is just an HTTP handler object.

The ProcessRequest method that finally calls IHttpHandler's Page object by the HttpRuntime object. HttpHandler processes the request based on the extension of the user request file and sends the result of the request, which is HTML, to the client's browser.

In the MVC source code, the Mvchandler class inherits the interface IHttpHandler and implements the ProcessRequest (HttpContext HttpContext) method, which is the entry of the MVC program.

10: At this point, it is very important to know how the route in MVC is associated with HttpApplication, urlroutingmodule how to intercept Httpapplicatioin pipeline events, so that the HTTP Request into the MVC framework.

In the UrlRoutingModule.cs file, you see that when UrlRoutingModule initializes and calls the Init method, the HttpApplication Postresolverequestcache pipeline event is registered. So when the Httpaplication object (here is Mvcapplication) executes, the Postresolverequestcache event is triggered, and the HttpRequest is booted into the MVC module

The first part involved in the ASP is UrlRoutingModule, which is part of the System.Web.Routing. UrlRoutingModule is used to check the URL of the request for the first time and whether the files on the local disk match. If matched, UrlRoutingModule will send the request directly to Iis,iis according to the address to handle accordingly. If UrlRoutingModule does not find a matching file on the disk. It examines the routecollection structure to determine whether the request continues to be delivered. UrlRoutingModule introduces Routehandler and matching path portals (Mvcroutehandler by default). The appropriate HttpHandler are then introduced to process and request the relevant logic. By default, this HttpHandler will be mvchandler.

How the 11:url routing component is combined with the ASP.

When starting the MVC program for the first time, register routing registerroutes (routetable.routes) in the Application_Start function, Routes. MapRoute (... ) function, register the custom routing rule in the System.Web.Routing component, route route = new route (URL, New Mvcroutehandler ())

Note: The following event will be performed by the HttpApplication class when processing the request. Developers who want to extend the HttpApplication class need to be aware of these events in particular.

  1. Validation of the request checks the information sent by the browser and determines whether it contains potentially malicious tokens. For more information, see ValidateRequest and Scripting Intrusion Overview.

  2. If any URLs have been configured in the Urlmappingssection section of the Web. config file, the URL mapping is performed.

  3. Raises the BeginRequest event.

  4. Raises the AuthenticateRequest event.

  5. Raises the Postauthenticaterequest event.

  6. Raises the AuthorizeRequest event.

  7. Raises the Postauthorizerequest event.

  8. Raises the Resolverequestcache event.

  9. Raises the Postresolverequestcache event.

  10. Depending on the file name extension of the requested resource (mapped in the application's configuration file), select the class that implements IHttpHandler to process the request. If the request is for an object (page) derived from the page class, and the page needs to be compiled, ASP. NET compiles it before creating an instance of the page.

  11. Raises the Postmaprequesthandler event.

  12. Raises the AcquireRequestState event.

  13. Raises the Postacquirerequeststate event.

  14. Raises the PreRequestHandlerExecute event.

  15. Call the appropriate IHttpHandler class's ProcessRequest method (or asynchronous version BeginProcessRequest) for the request. For example, if the request is for a page, the current page instance will process the request.

  16. Raises the PostRequestHandlerExecute event.

  17. Raises the ReleaseRequestState event.

  18. Raises the Postreleaserequeststate event.

  19. If the Filter property is defined, the response filter is executed.

  20. Raises the Updaterequestcache event.

  21. Raises the Postupdaterequestcache event.

  22. Raises the EndRequest event.

When we make a request to the ASP. NET MVC site, there are 5 main steps:
Step 1: Create the RouteTable the first step occurs when the ASP. NET application starts for the first time. RouteTable maps the URL to handler.
Step 2:urlroutingmodule Interception Request the second step occurs when we initiate the request. UrlRoutingModule intercepts each request and creates and executes the appropriate handler.
Step 3: Execute Mvchandler mvchandler The controller is created, and the controller is passed into the ControllerContext, then the controller is executed.
Step 4: Execute the Controller controller to detect the Controller method to be executed, build the parameter list and execute the method.
Step 5: Call the Renderview method in most cases, the Controller method calls Renderview () to render the content back to the browser. The Controller.renderview () method delegates the work to a viewengine.

This knowledge and images are summarized in the following blog post for easy viewing

Reference:

http://blog.csdn.net/jilate/article/details/437506

Http://www.cnblogs.com/tmfc/archive/2006/08/29/489779.html

http://blog.csdn.net/virone/article/details/4531800

Http://msdn.microsoft.com/zh-cn/library/ms178473%28v=VS.80%29.aspx

http://blog.csdn.net/wx845204140/article/details/7194743

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.