This article is a mirror article of machine translation, please click here to jump to the original article.

View: 51405|Reply: 5

[ASP.NET] asp.net IHttpModule interface is explained in detail

[Copy link]
Posted on 3/13/2018 9:46:06 PM | | | |
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.





Previous:Asp.Net IHttpHandler introduced
Next:C# must modify this header workaround with the appropriate attributes or methods
 Landlord| Posted on 3/13/2018 9:46:47 PM |
. NETFramework 4.5 provides 25 standard events.


 Landlord| Posted on 3/13/2018 9:47:03 PM |
. NETFramework 4.0, which provides 19 standard events.

1. BeginRequest: The first event of asp.net to start processing the request, indicating the start of processing.

2. AuthenticateRequest: Authentication request, generally used to obtain the user information of the request.

3. PostAuthenticateRequest: The user information that has been requested.

4. AuthorizeRequest: Authorization, generally used to check whether the user's request has been granted permission.

5. PostAuthorizeRequest: The user request has been authorized.

6. ResolveRequestCache: Get the processing results of the previous processing cache, if it has been cached before, then there is no need to process the request, and directly return the cached results.

7. PostResolveRequestCache: The cache has been processed.

8. PostMapRequestHandler: The requested processor object has been created based on the user's request.

9. AcquireRequestState: Obtain the status of the request, which is generally used for session

10. PostAcquireRequestState: Session has been obtained

11. PreRequestHandlerExecute: Prepare the execution handler.

12. PostRequestHandlerExecute: The handler has been executed

13. ReleaseRequestState: The state of the release request.

14. PostReleaseRequestState: The status of the request has been released.

15. UpdateRequestCache: Update the cache.

16. PostUpdateRequestCache: The cache has been updated.

17. LogRequest: The log operation requested

18. PostLogRequest: The log operation of the request has been completed.

19. EndRequest: The request is processed.
Posted on 4/22/2019 9:48:33 AM |
Bookmark it
Posted on 9/30/2021 7:19:09 PM |
Recently studying. Read it carefully
 Landlord| Posted on 7/14/2023 8:26:36 PM |
ASP.NET MVC counts requests based on IHttpModule modules
https://www.itsvse.com/thread-10627-1-1.html
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com