Learn how IIS & MVC works

Source: Internet
Author: User

I have been wondering how a request sent from a client connects to IIS on the server side, and how IIS reads the code I published, and return the files on the server. What is the processing process.

1: When you enter an address in your browser or click a link, you have sent an HTTP request (the site is located based on the host header, IP address, or port number of the request );

2: According to the HTTP protocol, when a request arrives at the corresponding host server, it is received by the system process HTTP. sys on the server (which can be understood as a process specially processing HTTP requests;

3: After HTTP. sys receives the request signal, it passes it to the worker process in the application pool, that is, the IIS process inetinfo.exe,Note: At this time, the server process andIISConnect.

Iisprogress can process general static pages such as. html. After processing, the HTML page is directly returned to the client for display;

4: If it is a page such as. aspx or cshtml, IIS cannot process it directly. In this case, the IIS process will load a page called aspnet_isapi.dll;

5: When the ISAPI receives the Processing request, it starts an ASP. net workflow and forwards the request information to the ASP. NET workflow (aspnet_wp.exe ),Note: At this timeIISAnd ASP. NETConnect.Next, the control is controlled by aspnet_wp.

 

[Let's first understand what appdomain is, feel this good http://allanpie.blog.163.com/blog/static/21320410200921311273632/, I understand appdomain is a can process ASPnet process (. DLL) environment or application domain, we know ASP. to run the. NET program.. NET Framework is a basic platform and can be run only after it is hosted by the CLR (Common Language Runtime Library. ASP. NET is a complex engine that uses hosted code to process Web requests from start to end.

Appdomain contains several important objects.

Isapiruntime: responsible for solving the necessary information of the HTTP request, which forwards the information and request to httpruntime.

Httpruntime: the main task is to create an httpcontext object for each customer who initiates a request. This stuff manages the httpsession object.

Each visitor has their own httpcontext object and httpsession object. You can find the corresponding class name in the. NET Framework library, such as system. Web. httpcontext, system. Web.

Httpsessionstate and so on. These two objects are part of the appdomain runtime environment. These two classes are called system. Web. Hosting. isapiruntime, and system. Web. httpruntime .]

 

6: If the control is left to the aspnet_wp process. in the. NET environment, the appdomain will be created first to execute the application domain. When the appdomain is created, the isapiruntime and httpruntime classes will be instantiated as part of the appdomain.

7: After the appdomain Initialization is complete, you need to establish a session to pass the request to one of the classes httpruntime. The relationship between the classes is: httpruntime is responsible for creating httpcontext and httpsession,

Httpcontext is responsible for managing httpsessions.

 

From the time when an HTTP request is submitted to this (httpcontext is created after httpruntime), the application we released on the server is still not running, or, the requester's request is not actually processed, and the previous work is both preparation or auxiliary work.

8: Next, in addition to creating httpcontext and httpsession, httpruntime also creates an important object httpapplication.

Httpapplication calls the processrequest method to process user requests. Specifically, it creates an httpcontext instance,

9: httpruntime searches for or creates an object for a Web application that can process the request using context information. Httpapplication factory is responsible for returning the httpapplication instance. The httpapplication object uses an ihttphandlerfactory-type instance to return httphandler (HTTP handler) to the httpruntime object. A page is just an HTTP handler object.

Finally, the httpruntime object calls the processrequest method of the ihttphandler Page Object. Httphandler processes the request according to the extension of the user request file, and sends the request result, that is, HTML, to the customer's browser.

In the MVC source code, the mvchandler class inherits the ihttphandler interface and implements the processrequest (httpcontext) method, which is the entry for MVC program running.

 

10: At this time, I would like to know how the routing in MVC is linked with httpapplication. How does the urlroutingmodule intercept the pipeline event of httpapplicatioin and introduce HTTP request into the MVC framework.

In urlroutingmodule. in the CS file, we can see that when the urlroutingmodule initializes and calls the init method, it registers the httpapplication's postresolverequestcache pipeline event. Therefore, when the httpaplication object (mvcapplication) is executed, a postresolverequestcache event is triggered, so as to direct httprequest to the MVC Module

The first part involved in ASP. net mvc is urlroutingmodule, which is part of system. Web. routing. Urlroutingmodule is used to check whether the requested URL matches the file on the local disk for the first time. If the request matches, urlroutingmodule sends the request directly back to IIS. IIS processes the request based on the address. If the urlroutingmodule does not find a matching file on the disk. It checks the routecollection structure to determine whether to continue the transfer. urlroutingmodule introduces routehandler and the matched path entry (mvcroutehandler by default ). Then, appropriate httphandler will be introduced to process and request-related logic. By default, the httphandler will be mvchandler.

 

11: How is the URL routing component combined with the ASP. net mvc framework.

When the MVC program is started for the first time, register the routing registerroutes (routetable. routes) in the application_start function; in routes. maproute (...) Function, register the custom routing rules to the system. Web. Routing component, route = new route (URL, new mvcroutehandler ())

 

Note: When processing this requestHttpapplicationClass to execute the following events. Want to expandHttpapplicationClass developers need to pay special attention to these events.

  1. When a request is verified, the browser checks the information sent and determines whether it contains a potentially malicious flag. For more information, see validaterequest and script intrusion overview.

  2. If you have configured any URL in the urlmappingssection of the web. config file, perform URL ing.

  3. The beginrequest event is triggered.

  4. The authenticaterequest event is triggered.

  5. This triggers the postauthenticaterequest event.

  6. The authorizerequest event is triggered.

  7. This triggers the postauthorizerequest event.

  8. This triggers a resolverequestcache event.

  9. The postresolverequestcache event is triggered.

  10. Based on the file extension of the requested resource (ing in the application configuration file), select an ihttphandler class to process the request. If the request is for an object (PAGE) derived from the page class and needs to compile the page, ASP. NET will compile the page before creating an instance.

  11. The postmaprequesthandler event is triggered.

  12. The acquirerequeststate event is triggered.

  13. This triggers the postacquirerequeststate event.

  14. Raises the prerequesthandlerexecute event.

  15. Call the appropriateIhttphandlerClass processrequest method (or asynchronous beginprocessrequest ). For example, if the request is for a page, the current page instance processes the request.

  16. The postrequesthandlerexecute event is triggered.

  17. This triggers the releaserequeststate event.

  18. The postreleaserequeststate event is triggered.

  19. If the filter attribute is defined, the response is filtered.

  20. The updaterequestcache event is triggered.

  21. This triggers the postupdaterequestcache event.

  22. An endrequest event is triggered.

 

When we send a request to the Asp.net MVC website, five main steps are taken:
Step 1: Create a routetable when the Asp.net application is started for the first time. Routetable maps URLs to handler.
Step 2: urlroutingmodule intercepts the request. Step 2 occurs when we initiate the request. The urlroutingmodule intercepts every request and creates and executes a suitable handler.
Step 3: Execute mvchandler to create a controller, pass the controller into controllercontext, and then execute the controller.
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 present the content back to the browser. The Controller. renderview () method delegates this job to a viewengine.

 

 

 

 

 

 

These knowledge and images are summarized in the following blog to facilitate viewing.

Refer:

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.