Written in front
Regarding the relevant content of IHttpModule, I was also asked during the interview, and I vaguely felt that this interface has an Init method, which can register a series of events with the Init method in the implementation class. At home this weekend, there is really nothing to do, even if it is to check and fill in the gaps in this piece of knowledge.
IHttpModule works
Friends who are familiar with asp.net life cycle should know that the execution of HttpModule is executed before HttpHandler, and after executing a series of events of HttpModule, then executing HttpHandler, and then executing some events of HttpModule. For details, please refer to the following life cycle diagram.
The HttpHandler is the place to handle the http request, the HttpModule is the "only way" for an HTTP request, so you can add some required information on top of the HTTP request information before this HTTP request is passed to the real request processing center (HttpHandler), or do some additional work on the intercepted HTTP request information. Or in some cases, simply terminate HTTP requests that meet some conditions, which can act as a filter.
An HTTP request will be passed to the HttpHandler container at some point (ResolveRequestCache event) during the delivery of the HttpModule container. After this event, the HttpModule container creates an ingress instance of the HttpHandler, but instead of giving up control of the HTTP request, it continues to fire the AcquireRequestState and PreRequestHandlerExcute events. After the PreRequestHandlerExcute event, the HttpModule window temporarily gives control to the HttpHandler container for real HTTP request processing.
Inside the HttpHandler container, the ProcessRequest method is executed to handle HTTP requests. After the container HttpHandler processes the entire HTTP request, it returns control to the HttpModule, which continues to pass the processed HTTP request information flow layer by layer until it is returned to the client.
An instance
Project structure
MyHttpModule code
Register a custom HttpModule in web.config
Browse page Default.aspx
So what is the order of execution of a series of events in the life cycle?
Browse the results
Use HttpModule to terminate the Http request
outcome
summary
Here we introduce the most important interface in the asp.net lifecycle, IHttpModule, which can be described as an event interface, because in the Init method in the implementation class, various events in the lifecycle can be registered, and various logic can be defined in the event.
|