In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to analyze and apply HttpHandler and HttpModule. The content of the article is of high quality, so Xiaobian shares it with you for reference. I hope you have a certain understanding of relevant knowledge after reading this article.
Mysterious HttpHandler and HttpModule
University time I am from drag control to start learning ASP.NET, many of the class library objects on. Net are not very well understood. So see everyone write some personality ASP.NET noun, feel ASP.NET always has a layer of mysterious veil shrouded, let me figure out, believe that there are many garden and I experienced similar. Net siege division. HttpHandler and HttpModule were mysterious in the past. Haha, today I will show you my understanding of him and his application.
Maybe you don't understand HttpHandler and HttpModule(Hero Return), maybe you don't know the purpose of HttpHandler and HttpModule, maybe you don't understand. Today, please let me lead you to appreciate the style of HttpHandler and HttpModule, today I want to make him So Easy !!
Understanding ASP.NET pipeline events
What is an ASP.NET pipeline? Simply put, the lifecycle of a page is a series of events that ASP.NET does from the time you request it to the time it appears in your browser. Here are some of these events for you (see also Fish Li).
1. The request is validated by examining the information sent by the browser and determining whether it contains potentially malicious tags. For more information, see ValidateRequest and Script Intrusion Overview.
2. If any URLs have been configured in the UrlMappingsSection of the Web.Config file, URL mapping is performed.
3. Raises the BeginRequest event.
4. Raises the AuthenticateRequest event.
5. Raises the PostAuthenticateRequest event.
6. Raises AuthorizeRequest event.
7. Raises the PostAuthorizeRequest event.
8. Raises the ResolveRequestCache event.
9. Raises the PostResolveRequestCache event.
10. Based on the file extension of the requested resource (mapped in the application's configuration file), select the class that implements IHttpHandler to process the request. If the request is for an object (page) derived from the Page class and the page needs to be compiled, ASP.NET compiles the page before creating an instance of it.
11. Raises the PostMapRequestHandler event.
12. Raises the AcquireRequestState event.
13. Raises the PostAcquireRequestState event.
14. Raises the PreRequestHandlerExecute event.
15. Call the ProcessRequest method of the appropriate IHttpHandler class (or asynchronous version of IHttpAsyncHandler.BeginProcessRequest) for the request. For example, if the request is for a page, the current page instance will process the request.
16. Raises the PostRequestHandlerExecute event.
17. Raises the ReleaseRequestState event.
18. Raises the PostReleaseRequestState event.
19. If the Filter property is defined, response filtering is performed.
20. Raises the UpdateRequestCache event.
21. Raises the PostUpdateRequestCache event.
22. Raises the EndRequest event.
23. Raises the PreSendRequestHeaders event.
24. Raises the PreSendRequestContent event.
Note:
1. Remember that these events are not written blindly, and their order is not written blindly. From the start of the page request to the end of the page display, they are triggered from one to twenty-four, from top to bottom.
2. Not every event starting with BeginRequest will be triggered, because Response.End() can be called at any time during processing, or an unhandled exception can occur and end the process prematurely. Of all the events, only the EndRequest event is definitely triggered, and BeginRequest (part of Module) may or may not be triggered.
3. In IIS7, the tenth event is the MapRequestHandler event, and two more events are added before the EndRequest event: LogRequest and PostLogRequest. MapRequestHandler, LogRequest, and PostLogRequest events are supported only if the application is running in IIS 7.0 integrated mode and with. NET Framework 3.0 or later.
Summary: We can add methods, classes, attributes, etc. to these events at will, which belong to your own operation on the request. So we used to program at the page level, and now we can process projects at the request level, process requests. How to do it depends on the magical effects of HttpMoudle and HttpHandler below.
Understanding HttpHandler and HttpModule
Let's start with HttpHandler.
First of all, you should understand how ASP.NET handles our request files. This is not to mention the seemingly lower level mystery of asp.net. So how does ASP.NET handle our request files? Let me show you something.
Open the web.config file on your computer in the C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\CONFIG\directory. Find the httpHandlers node and see what it says below. If you don't want to open it, watch me.
The above code is understood as: mapping different file types to different handlers in the node to handle, for.aspx, it is handled by System.Web.UI.PageHandlerFactory. For.cs, it is handled by System.Web.HttpForbiddenHandler...
The above is the default Handler processing, of course, after knowing HttpHandler, we can also register our own HttpHandler, write our own HttpHandler handler, handle different types of files, this I will implement later, to show you.
Question: Since the function of HttpHandler is to map files with different type suffixes in the request to different handlers for processing, at what time in the page life cycle does the handler map the request?
A: According to the 24 events above, the mapping in this HttpHandler node is triggered in step 10. It maps to different handlers in which the implementation of methods and classes is triggered in step 15.
HttpModule.
HttpHandler is a type of file that is mapped to a specified handler for processing requests. And the mapping, and processing, takes place in events that ASP.NET has specified.
HttpModule maps all request files to specified handlers to process requests, which can occur in any event in the request pipeline. That is to say, which event you subscribe to, the write process will occur in that event, and then execute after processing. The next event of the event you subscribed to, of course, you can also terminate all events and run *** an event directly. This means that he can not give HttpHandler a chance. Very powerful HttpModule.
Use of HttpHandler
The use of HttpHandler is demonstrated by an anti-theft chain technology
1. Register HttpHandler first: Register in Web.config
The above registration maps requests for jpg files from websites to assemblies ProcessHandler_test with namespace httphander_test and class name CustomHandler to process the requests.
2. If you want to process requests through HttpHandler, you must implement the interface IHttpHandler in the mapped handler.
3. The program code mapped to is as follows
namespace httphander_test { public class CustomHandler :IHttpHandler { public void ProcessRequest(HttpContext context) { //Get file server-side physical path string FileName = context.Server.MapPath(context.Request.FilePath); //If UrlReferrer is empty, display a default image with no stolen links if (context.Request.UrlReferrer.Host == null) { context.Response.ContentType = "image/gif"; context.Response.WriteFile("/error.gif"); } else { //If UrlReferrer does not contain your own host domain name, a default image with no link theft is displayed. if (context.Request.UrlReferrer.Host.IndexOf("yourdomain.com") > 0) { context.Response.ContentType = "image/gif"; context.Response.WriteFile(FileName); } else { context.Response.ContentType = "image/gif"; context.Response.WriteFile("/error.gif"); } } } public bool IsReusable { get { throw new NotImplementedException(); } } } }
The simple example above is complete. If there is a request for a Jpg file, rather than a request in the domain name of this site, a specified error image will be output to replace the original link image.
Summary: httpHandler's functions are far more than these, I hope you can understand that he is processing a type of file request, and I hope you can understand his event location in the request pipeline, which will be more helpful for your understanding.
Use of HttpModule
Because HttpModule is too powerful, that is to say, any request must go through the registered HttpModule handler, so when you use it, you must make a good judgment on various requests, that is, what request to handle, let this request go through that handler, do not let him execute every method. If you don't let the program load, you lose more than you gain.
Using HttpModule is similar to HttpHandler, while HttpModule implements the IHttpModule interface.
About how to carry out HttpHandler and HttpModule analysis and application to share here, I hope the above content can be of some help to everyone, you can learn more knowledge. If you think the article is good, you can share it so that more people can see it.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.