ASP. NET Processing Event

Source: Internet
Author: User

Never, ever, never studied this event. But to write that CGI-style interface, you have to study it.

Environmental W10 VS2017

Test method: Write a class that implements the IHttpModule interface, load all events in the Init method, and then play the log to see the order of event execution.

The module interface class needs to be used after you have opened module, add module, in IIS. or add a module node to the Webconfig.

The event execution results are as follows:

  • 0 occurs as the first event in an HTTP execution pipeline chain when ASP. NET responds to a request. BeginRequest
  • 1 authenticaterequest occurs when the security module establishes a user ID
  • 2 occurs when the security module has established a user ID. Postauthenticaterequest
  • 3 occurs when the security module has authenticated user authorization. AuthorizeRequest
  • 4 occurs when the currently requested user is authorized. Postauthorizerequest
  • 5 occurs when an ASP. NET finishes authorization event to make the Cache module service the request from the cache, bypassing the execution of an event handler, such as a page or XML Web services. Resolverequestcache
  • 6 occurs when ASP. NET skips execution of the current event handler and allows the cache module to satisfy requests from the cache. Postresolverequestcache
  • 7 occurs when the handler used to respond to the request is selected Maprequesthandler
  • 8 occurs when ASP. NET has mapped the current request to the appropriate event handler. Postmaprequesthandler
  • 9 occurs when ASP. NET gets the current state (such as session state) associated with the current request. AcquireRequestState
  • 10 occurs when a request state (such as session state) has been acquired associated with the current request. Postacquirerequeststate
  • 11 occurs just before ASP. NET begins executing an event handler (for example, a page or an XML Web services). PreRequestHandlerExecute
  • 12 error occurs when an unhandled exception is thrown
  • 13 occurs when an ASP. NET event handler (for example, a page or an XML Web service) finishes executing. PostRequestHandlerExecute
  • 14 occurs after all request event handlers have been executed by ASP. This event causes the state module to save the current state data. ReleaseRequestState
  • 15 occurs when ASP. NET has completed execution of all request event handlers and the request state data has been stored. Postreleaserequeststate
  • 16 occurs when ASP. NET executes the event handler so that the cache module stores the response that will be used to serve the subsequent requests from the cache. Updaterequestcache
  • 17 This event occurs after ASP. NET completes the update of the cache module and stores the response to service the subsequent requests from the cache. Postupdaterequestcache
  • 18 occurs just before any records are executed for the current request by ASP. Logrequest
  • 19 occurs after ASP. NET finishes processing all event handlers for the System.Web.HttpApplication.LogRequest event. Postlogrequest
  • 20 occurs as the last event in an HTTP execution pipeline chain when ASP. NET responds to a request. EndRequest
  • 21 occurs just before the HTTP header is sent to the client by ASP. Presendrequestheaders
  • 22 occurs exactly before ASP. NET sends content to the client. Presendrequestcontent

A total of 25 events, the log shows 23. There are 2 not typed.

Disposed//Occurs when the application is released. ----can also invoke the Dispose () method itself to raise the event, preferably in the EndRequest event, after which the Application object is not available.

requestcompleted//occurs when a managed object is associated with a request that has already been disposed. ----This doesn't know how to trigger.

in order for a class method to become an interface :(reference blog http://www.cnblogs.com/fish-li/archive/2011/09/05/2168073.html)

    1. Use the IHttpModule module: A method for dynamically invoking this class in a module. Simply say that you find the class name and method name based on the URL address, then call them with "reflection" and pass the context object to this method.
    2. Treated with IHttpHandler: The same method as above.

According to the recommendations of the Reference blog, the use of the IHttpHandler method is good. I read this question a few times before I got some idea.

------doubts are always wondering where the difference between IHttpModule and IHttpHandler is. Why do you want to provide these two interfaces. It now seems to be possible to find out the reason for the design of this pipeline event.------

Design of pipeline treatment method:

Since the design handles the event, which means that the processing of an HTTP request is rigidly defined as these steps, each step performs a custom operation in the manner in which the event is registered. These steps are justified and need to be asked by the designer. These events are also adjusted, and there are no 25 events from previous versions of ASP. It looks like 19. In this way, there is no reasonable, only constant tuning. Before writing the snake game, use a similar method, in the snake every move this moment, decomposition into a few steps, determine the state of the snake, and then execute the corresponding code. There is a good thing about this is that it's easy to extend the functionality, like adding a new state Just find the right steps and register the method with this step. Of course, the premise is that these steps are designed to be "reasonable".

IHttpModule and IHttpHandler Timing

The role of IHttpModule is to register a pipeline event, and "all requests" will be executed for events registered in this class (which may be wrong). While IHttpHandler is a handler, it is part of the pipeline process. Print Log discovery:

After the 11th event, execution IHttpHandler//(11 happens just before ASP. NET begins executing an event handler (for example, a page or an XML Web services). ) PreRequestHandlerExecute

Also after the 7th event, execute ihttphandlerfactory (this is used to specify which IHttpHandler to use, see the Reference blog)//7 occurs when the handler that is used to respond to the request is selected Maprequesthandler

HttpApplication class Management event handling

The HttpApplication class manages the operation of these events, and it has a method completerequest (); // causes ASP. To skip HTTP to execute all events and filters in the pipeline chain and execute the EndRequest event directly .

If this method is called in the first event, the BeginRequest event. Then the program prints the log as follows:

    • 0 occurs as the first event in an HTTP execution pipeline chain when ASP. NET responds to a request. BeginRequest
    • 18 occurs just before any records are executed for the current request by ASP. Logrequest
    • 19 occurs after ASP. NET finishes processing all event handlers for the System.Web.HttpApplication.LogRequest event. Postlogrequest
    • 20 occurs as the last event in an HTTP execution pipeline chain when ASP. NET responds to a request. EndRequest
    • 21 occurs just before the HTTP header is sent to the client by ASP. Presendrequestheaders
    • 22 occurs exactly before ASP. NET sends content to the client. Presendrequestcontent

The 1th and last 5 events are triggered, and the IHttpHandler needs to be triggered after the 11th event, so there is no opportunity to execute. This shows that IHttpHandler is one of the links in the pipeline process.

A pipeline event can be skipped without execution, so if it is a system that takes full advantage of the ASP. NET-pipe event mechanism, many of its code is registered at each event step, so skip it for sure. Because there is no guarantee that events registered by other modules will be executed.

If you do not use these events at all and do not use these modules, just " to make a method of a class become an interface ", then the event may not be registered either. The module can not be loaded. But the handler must be IHttpHandler.

module and handler mapping settings (For IIS modules see: https://docs.microsoft.com/en-us/iis/get-started/introduction-to-iis/ Introduction-to-iis-architecture)

After you create a new Web site in IIS (Integrated mode. NET4.0). Many modules and handler mappings have been loaded. You can delete unwanted modules (if they are inherited, delete them, you need to open "modules" on the IIS node, and then select the module, and click Unlock.) to the site can be deleted)

  Important Modules:

StaticFileModule (default, native, inherited)

Used to process static files, html,js,css. And so on. If not added, static files cannot be accessed.

Directorylistingmodule (default, native, inherited)

The Directory browsing feature, if not added, allows directory browsing permissions where the directory list is not visible. Few people now browse the directory under the site. Unless it's an FTP

Defaultdocumentmodule (default, native, inherited)

The default document, such as Http://www.xx/home, displays the default index.htm (if this default document is set). If not added, then this function is not required. You must enter the full http://www.xx/home/index.html

Anonymousauthenticationmodule (default, native, inherited)

Anonymous authentication. If not added, the webpage does not open, prompting no permission 401 error. and triggers a 2nd pipe event (AuthenticateRequest occurs when the security module has established a user ID), triggering the 18-22 event immediately. The effect is like calling a CompleteRequest ()

  Important Handler mappings:

Staticfile Request Path: * Module: Staticfilemodule,defaultdocumentmodule,directorylistingmodule permissions: Read, script request limit: Read

If you do not have this mapping: static files cannot be accessed. Hint 404 Not Found

The relationship between the module and the handler:

Is there a problem between the processing program and the module?

A. Handler mappings: (In IIS Features, "Handler mappings" double-click to open) you can add several

1. "Add managed Handler", this will set "request Path", "type", request path can be *,*.ext,*.ashx, also can have directory such as/api/*. In short, a path rule, "type" is the class name of this handler (with namespace), Request restrictions can also be set to call this handler, generally do not check the mapping option. Once set, the handler is executed whenever a request conforms to this path rule. The program class must implement the IHttpHandler interface.

2. "Add Module mapping", this will set the "Request Path", module, in the Module drop-down list only the native installed modules, if you want to own modules, you need to double-click on the IIS root module, configure the native module, and register this place to add. Then the "module" in the site After adding this native module to the function, the drop-down list is only. (I use C # to write a class that implements the IHttpModule interface after compiling it into a DLL, registering.) found no effect. I don't know what the reason is.

The above two settings, feel as follows, IHttpModule and IHttpHandler can handle the request, and can let the IHttpHandler handler is responsible for all requests of a certain kind of path rule (1), also can specify the module processing (2) for the request of another path rule.

  So that is, the module is no longer for all requests, then the event registered in this module can be only for a certain type of request. (This could be a misunderstanding, probably a big difference between native and managed modules)

B. Module: (In IIS Function, "module")

Add refers to a managed module (that implements the IHttpModule interface), with an option to "call only for requests made to an ASP." or a managed handler. There is a difference between checking and not checking. If you select, when you request a static file, the discovery module does not execute, and when you request a handler, module is not executed. If there is no selection, then it executes when the static file is processed. This indicates that the module registered events are valid for all requests.

This option is to solve an obvious problem, that is, "if the module is for all requests," then the function of the module will be executed, but in most cases, the static file is sent directly to the client, do not need to execute the module event. No pipeline processing is required.

I. The above may indicate that a managed module or handler is actually designed for a request and does not require a module or handler that takes over all the requests. At least the processing of static and dynamic files is different, staticfile the path used by this module map is *, Indicates that it takes over all requests and uses the Defaultdocumentmodule,directorylistingmodule,staticfilemodule three modules to process these requests. Then the other path rules should be executed before it, If you can't find it, throw it as a static file processing (guess).

Ii. Summary:

Managed module IHttpModule The events registered are for all requests, but when added, you can choose to target only the managed request (or the request that indicates the handler). Its role is to pay attention to various events, not specifically processing requests and return results. (although it is possible)

Handler IHttpHandler is a step in several events that is performed after 11 steps. Its important task is to return a processing result. You don't have to care about where you are in handling events.

III. If you do not use this set of processing piping mechanism, then there is no need to use ASP. You can use System.Net. HttpListener This class to write a listener and then hang up your C # program.

In order for a class method to become an interface, you can:

Customize a Web site, load only the necessary modules and handler mappings, then put your own class library project, and then get a Web. config (this can be easily configured with the module and mapping, without the need to configure on the IIS console) into this site directory, execute the program put bin.web.config root directory , this is possible.

------------------------------------Revise and supplement it at any time-----------------------------------

ASP. NET Processing Event

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.