ASP.NET must know the stuff (HttpModule, HttpHandler)

asp.net architecture

Asp.net process a request ------------------- HttpModule HttpHandler that must be mastered something that must be mastered something very useful two or more instances --- ------------------ asp.net event model mechanism

----------------------- page to a customer's request is processed by aspnet_isapi.dll this dynamic link library, the request is sent to compile the aspx file to execute CLR and then flows back to the browser Html -------------------------- two page events execution order Page_Init: initialize values ​​or connect Page_Load: mainly used IsPostBack, the main event was to perform a series of operations to create the first page or asp.net response was caused by the delivery of client-side events. Prior to this event, the restored page and control view state. Page_DataBind: at the page level call can also be invoked in a single control. DataBind_PreRender: data binding pre-rendered, this event is fired just before the view state is saved and render the control. Page_Unload: This event is to perform final cleanup work. Non-deterministic events Page_Error: If an unhandled exception occurs during page processing, then the excitation error events. Page_AbortTransaction: transaction event, a transaction if the transaction has been aborted This event fires, common shopping cart. Page_CommitTransaction: If you have successfully fires when a transaction of this event. -------------------------------------------------- ------ Global.asax events (execution order) Application_Start: excited when the application starts Application_BeginRquest: http requests begin to stimulate Application_AuthenticateRequest: An application to stimulate Session_Start approving the request http: stimulate Application_EndRequest session startup: end Htttp request when excited Session_End: Application_End excited at the end of the session: to stimulate Application_Error at the end of the application: stimulate ---------------------- ISAPI error occurs: To insert some web server set up, expand capabilities, enhanced web server functionality. ISAPI: extension, win32 dynamic link library, such as aspnet_isapi.dll, ISAPI extensions can be seen as a normal application, it deals with the target HTTP request. ISAPI: filters, web server passes the request to the relevant filter, then the filter may modify the request, performs some operations and the like. ASP.NET process requested: the pipes based on models, in which the ASP.NET http request to the pipeline all the modules. Each module receives the HTTP request, and to have full control. Once the HTTP request goes through all the modules, eventually disposed HTTP handler. HTTP handler to request some processing, and the result will again through the HTTP pipeline module modules. ----------- httpmodule ISAPI filter (Filter): IIS itself does not support dynamic pages, meaning that he only supports static HTML pages of content for .asp .aspx .cgi .php etc., IIS does not know how to handle these suffixes, it will use it as text, did not deal sent to the client. To solve this problem, IIS has a mechanism called the ISAPI filter. It is a COM component. ASP.NET services registered to IIS when the file extension will be processed each extension to IIS registered inside (such as * .ascx * .aspx, etc.). After the expansion initiated, by definition good way to handle file IIS can not handle, then control jumps to process special handling code in, asp.net is aspnet_isapi.dll. Let the process begin processing code, generate standard HTML code, after generating the code added to the original HTML, the last full HTML returned to IIS, IIS then sends the content to the client. ---------------- HttpModule Http module of the functional filter (ISAPI filter), which is realized .net assembly System.Web.IHttpModule interface. . These components by registering itself in certain events, to insert themselves into the ASP.NET request processing pipeline. When these events occur, ASP. NET calls are interested in HTTP request module, so the module can process the request. Sometimes need to worry too much about the http request, note that it is not covered by other systems include built-in HttpModule, the configuration in Machine.config. -------------------------------------- HttpHandler It implements ISAPI Extention function, which processes the request (Request) and transmits a response message (response). By HttpHandler IHttpHandler function must implement the interface. HTTP handler is the interface implemented System.Web.IHttpHandler .NET components. Any class that implements this interface can be used to process the incoming Http request. It is Http handler.

In previous time ASP, * .asp page when a requested file, the HTTP request first be intercepted named inetinfo.exe process, this process is actually www service. After it will intercept this request to the asp.dll process, which will explain the asp page, and then explain the data stream back to the client browser. In fact ASP.DLL is a document attached to the IIS ISAPI, which is responsible for the interpretation of the implementation of the file, such as an ASP file, ASA, etc., ---------------------- ---------------

, With similar, the http request will be intercepted by the inetinfo.exe process (www service), it is judged that the file suffix after asp ASP.NET HTTP request processing method when a client requests a page * .aspx files to the web server, the request will be forwarded to the ASPNET_ISAPI.DLL and ASPNET_ISAPI.DLL, will be sent to ASPNET_WP.EXE process through a pipeline Http pipeLine this http request, when the HTTP request comes ASPNET_WP.EXE process, asp.net framework will pass HttpRuntime to deal with the Http request, after the processed results back to the client. ------------------------------------ When a http request is sent to HttpRuntime, the Http request will continue to be fed into a container which is called a HttpApplication Factory's, and this will give the vessel a http request HttpApplication instance to handle the incoming transmission, then the http request will turn into the following several containers: HttpModule -> HttpHandler Factory -> HttpHandler after ProcessRequest method of HttpHandler processed inside the system, the entire Http Request was processed, the client will give the corresponding stuff. Full http request in asp.net framework of a process flow: HttpRequest -> inetinfo.exe-> ASPNET_ISAPI.DLL -> Http Pipeline -> ASPNET_WP.EXE -> HttpRuntime -> HttpApplication Factory -> HttpApplication -> HttpModule -> HttpHandler Factory -> HttpHandler -> HttpHandler.ProcessRequest () If you want to intercept a httpRequest in the middle and do their own processing, it should HttpRuntime running inside to do it, when to do precisely this HttpModule in this container. ---------------------------------------- ---------- --------------------------- HttpModule system itself to achieve a IHttpModule interface, of course, our own classes can be achieved IHttpModule interfaces, which the system can replace the HttpModule objects. The system default ASP.NET HttpModule:

DefaultAuthenticationModule ensure Authentication object is present context. It can not be inherited. FileAuthorizationModule verify that the remote user has permission to access the requested NT file. It can not be inherited. FormsAuthenticationModule enable ASP.NET application to use Forms authentication. It can not be inherited. PassportAuthenticationModule surround PassportAuthentication provide packaging services. It can not be inherited. SessionStateModule provides session-state services for the application. UrlAuthorizationModule provide URL-based authorization service to permit or deny access to specific resources. It can not be inherited. WindowsAuthenticationModule enable ASP.NET applications to use Windows / IIS authentication. Can not be inherited

-------------------------------------- The system default HttpModule in the machine.config file configuration, and when we use to develop the relationship web.config is: Http request is to start a process of time, it will be followed by a request to load machine.config and web.config files in the directory where the page in ASP.NET FRAMEWORK, if the machine is configured with its own HttpModule, you can still remove out of this mapping in the web.config file is located page.

  1. public class HelloWorldModule : IHttpModule  
  2.   
  3. {  
  4.   
  5.     public HelloWorldModule()  
  6.   
  7.     {  
  8.   
  9.     }  
  10.   
  11.   
  12.   
  13.     public String ModuleName  
  14.   
  15.     {  
  16.   
  17.         get { return "HelloWorldModule"; }  
  18.   
  19.     }  
  20.   
  21.   
  22.   
  23.     // In the Init function, register for HttpApplication   
  24.   
  25.     // events by adding your handlers.  
  26.   
  27.     public void Init(HttpApplication application)  
  28.   
  29.     {  
  30.   
  31.         application.BeginRequest +=   
  32.   
  33.             (new EventHandler(this.Application_BeginRequest));  
  34.   
  35.         application.EndRequest +=   
  36.   
  37.             (new EventHandler(this.Application_EndRequest));  
  38.   
  39.     }  
  40.   
  41.   
  42.   
  43.     private void Application_BeginRequest(Object source,   
  44.   
  45.          EventArgs e)  
  46.   
  47.     {  
  48.   
  49.     // Create HttpApplication and HttpContext objects to access  
  50.   
  51.     // request and response properties.  
  52.   
  53.         HttpApplication application = (HttpApplication)source;  
  54.   
  55.         HttpContext context = application.Context;  
  56.   
  57.         context.Response.Write("<h1><font color=red> HelloWorldModule: Beginning of Request</font></h1><hr>");  
  58.   
  59.     }  
  60.   
  61.   
  62.   
  63.     private void Application_EndRequest(Object source, EventArgs e)  
  64.   
  65.     {  
  66.   
  67.         HttpApplication application = (HttpApplication)source;  
  68.   
  69.         HttpContext context = application.Context;  
  70.   
  71.         context.Response.Write("<hr><h1><font color=red>HelloWorldModule: End of Request</font></h1>");  
  72.   
  73.     }  
  74.   
  75.   
  76.   
  77.     public void  Dispose ()   
  78.   
  79.     {  
  80.   
  81.     }  
  82.   
  83. }  
  1. <system.web>  
  2.   
  3.  <httpModules>  
  4.   
  5.   <add name="HelloWorldModule" type="HelloWorldModule"/>  
  6.   
  7.  </httpModules>  
  8.   
  9.    </system.web>  

-------------------------------------------------- --------------------------------- depth HttpModule a Http request will turn to after being captured ASP.NET Framework HttpModule and HttpHandler to handle. After hm between hh and are not completely independent, in fact, http request during hm will pass within a certain event will control to the hh, but the real implementation of the process is completed in the HttpHandler, will again HttpHandler code for returning control of the above HttpModule HttpModule the Init () parameters are HttpApplication type, having a number of events, including BeginRequest, EndRequest, AuthentiacteRequest like. -------------------------------------------------- it is asp.net --------------- IHttpHandler Framework provides an interface, the system defines a number of conventions to be achieved if a process is required Http request must be implemented. That is, if you want to handle certain types of HTTP requests on their own information flow, you need to implement these systems in order to achieve agreement. For example a * .aspx file to handle this type of Http request, ASP.NET FRAMEWORK will to a class called System.Web.UI.PageHandlerFactory HttpHandler to handle. HH and HM the same, HttpHandler class system by the user in web.config ASP.NET FRAMEWORK most initial load first in machine.config HttpHandler, and then loads the directory where the Web application's custom. However, the relationship between the system and the HH is our custom, "coverage", that is to say if we customized for a "* .aspx" The HttpHandler class, then the system will handle this right entirely to the http request HttpHandler class definition to deal with our own, and our own HttpHandler class you need to own fully resolve this Http request and make a deal. The most important of IHttpHandler ProcessRequest method, this method is used to process a HttpHandler Http request, when a Http request after the time passed by the HttpModule HttpHandler container to container, framework calls ProcessRequest method of HttpHandler to do to make this Http request the real deal. framework is actually not related directly to the HTTP page request to locate on an internal default IHttpHandler container, but positioned on the inside of the default IHttpHandler Factory. IHttpHandler Factory is the role of the system has achieved a lot of IHttpHandler container scheduling and management, the advantages of doing so is greatly enhanced load of the system, to enhance efficiency.

Reproduced in: https: //www.cnblogs.com/Tim_Liu/archive/2011/04/26/2029477.html

Guess you like

Origin blog.csdn.net/weixin_33725272/article/details/94724553