About HttpHandler and HttpModule's understanding and Application methods _ practical skills

Source: Internet
Author: User
Tags what is asp

Mystical HttpHandler and HttpModule.

When I was in college, I started to learn asp.net from the drag control, and I don't know much about. NET's class library objects. So see everyone write some personality asp.net noun, feel asp.net always have a layer of mysterious veil shrouded, let me not think through, I believe there are many in the garden and I experienced similar. NET Siege division. In the past to see HttpHandler and HttpModule are mysterious and unpredictable. Haha, today I will show you my understanding of him, as well as the application.

Perhaps you do not understand HttpHandler and HttpModule (Warrior return), perhaps you do not know the use of HttpHandler and HttpModule, perhaps you indefinitely. Today, please let me lead you to a taste of HttpHandler and HttpModule style, today I want to make him so easy!!

Understanding ASP.net Pipeline Events

What is ASP.net pipeline? Simply put, the life cycle of a page is a series of events that the page asp.net from when you start asking for it to show up in your browser. Here's a show of these events (see with fish Li).

1. Validation of the request will examine the information sent by the browser and determine whether it contains potentially malicious markup. For more information, see ValidateRequest and Scripting Intrusion Overview.

2. If any URLs have been configured in the Urlmappingssection section of the web.config file, the URL mapping is performed.

3. Raise the BeginRequest event.

4. Raise the AuthenticateRequest event.

5. Raise the Postauthenticaterequest event.

6. Raise the AuthorizeRequest event.

7. Raise the Postauthorizerequest event.

8. Raise the Resolverequestcache event.

9. Raise the Postresolverequestcache event.

10. Depending on the file name extension of the requested resource (mapped in the application's configuration file), select the class that implements IHttpHandler and process the request. If the request is for an object that derives from the Page class

(page), and you need to compile the page, asp.net compiles it before the instance of the page is created.

11. Raise the Postmaprequesthandler event.

12. Raise the AcquireRequestState event.

13. Raise the Postacquirerequeststate event.

14. Raise the PreRequestHandlerExecute event.

15. Invoke the ProcessRequest method (or asynchronous version ihttpasynchandler.beginprocessrequest) of the appropriate IHttpHandler class for the request. For example, if the request is for a page, the

The current page instance will process the request.

16. Raise the PostRequestHandlerExecute event.

17. Raise the ReleaseRequestState event.

18. Raise the Postreleaserequeststate event.

19. If the Filter property is defined, a response filter is performed.

20. Raise the Updaterequestcache event.

21. Raise the Postupdaterequestcache event.

22. Raise the EndRequest event.

23. Raise the Presendrequestheaders event.

24. Raise the Presendrequestcontent event.

Note:

1. Remember that these events are not written blindly, nor are they written in a blind order. is the page from the start request to the end of the page display, they are from one to 24, from top to bottom, in turn triggered.

2. Events starting from BeginRequest, not every event is triggered, because throughout the process, you can call Response.End () at any time or have an unhandled exception occur and end the entire process prematurely. In all events, only the EndRequest event is bound to be triggered, and the beginrequest of (partially module) may not be triggered.

3. In the case of IIS7, the 10th event is the Maprequesthandler event, and another two events were added before the endrequest event: Logrequest and Postlogrequest. The Maprequesthandler, Logrequest, and Postlogrequest events are supported only when the application runs in IIS 7 set mode and runs with the. NET Framework 3.0 or later.

Summary: These events allow you to add methods, classes, attributes, and so on to the events you need to do the things you want to do with your request. In other words, we used to program at the page level, now we can process the project at the request level, processing the request. Specifically how to do, to see the following httpmoudle and HttpHandler magic effect.

Understanding HttpHandler and HttpModule

First say HttpHandler.

First you should understand how ASP.net handles our request files, and this is not about the seemingly more low-level mysteries of asp.net, so what does. NET do with our request files? I'll show you something.


Open the Web.config file in the C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\CONFIG\ directory on your computer. Find the httphandlers node and see what he has written underneath. If you don't want to open it, look at me.

Copy Code code as follows:

View Code

<add verb= "*" path= "*.rules" type= "System.Web.HttpForbiddenHandler" validate= "true"/>
<add verb= "*" path= "*.xoml" type= "System.ServiceModel.Activation.HttpHandler, System.ServiceModel, version= 3.0.0.0, Culture=neutral, publickeytoken=b77a5c561934e089 "validate=" false/>
<add path= "*.svc" verb= "type=" System.ServiceModel.Activation.HttpHandler, System.ServiceModel, version= 3.0.0.0, Culture=neutral, publickeytoken=b77a5c561934e089 "validate=" false/>
<add path= "Trace.axd" verb= "*" type= "System.Web.Handlers.TraceHandler" validate= "True"/>
<add path= "WebResource.axd" verb= "Get" type= "System.Web.Handlers.AssemblyResourceLoader" validate= "True"/>
<add path= "*.axd" verb= "*" type= "System.Web.HttpNotFoundHandler" validate= "True"/>
<add path= "*.aspx" verb= "*" type= "System.Web.UI.PageHandlerFactory" validate= "True"/>
<add path= "*.ashx" verb= "*" type= "System.Web.UI.SimpleHandlerFactory" validate= "True"/>
<add path= "*.asmx" verb= "type=" System.Web.Services.Protocols.WebServiceHandlerFactory, System.Web.Services, version=2.0.0.0, Culture=neutral, publickeytoken=b03f5f7f11d50a3a "validate=" False/>
<add path= "*.rem" verb= "type=" System.Runtime.Remoting.Channels.Http.HttpRemotingHandlerFactory, System.Runtime.Remoting, version=2.0.0.0, Culture=neutral, publickeytoken=b77a5c561934e089 "validate=" False "/>
<add path= "*.soap" verb= "type=" System.Runtime.Remoting.Channels.Http.HttpRemotingHandlerFactory, System.Runtime.Remoting, version=2.0.0.0, Culture=neutral, publickeytoken=b77a5c561934e089 "validate=" False "/>
<add path= "*.asax" verb= "*" type= "System.Web.HttpForbiddenHandler" validate= "True"/>
<add path= "*.ascx" verb= "*" type= "System.Web.HttpForbiddenHandler" validate= "True"/>
<add path= "*.master" verb= "*" type= "System.Web.HttpForbiddenHandler" validate= "True"/>
<add path= "*.skin" verb= "*" type= "System.Web.HttpForbiddenHandler" validate= "True"/>
<add path= "*.browser" verb= "*" type= "System.Web.HttpForbiddenHandler" validate= "True"/>
<add path= "*.sitemap" verb= "*" type= "System.Web.HttpForbiddenHandler" validate= "True"/>
<add path= "*.dll.config" verb= "Get,head" type= "System.Web.StaticFileHandler" validate= "True"/>
<add path= "*.exe.config" verb= "Get,head" type= "System.Web.StaticFileHandler" validate= "True"/>
<add path= "*.config" verb= "*" type= "System.Web.HttpForbiddenHandler" validate= "True"/>
<add path= "*.cs" verb= "*" type= "System.Web.HttpForbiddenHandler" validate= "True"/>
<add path= "*.csproj" verb= "*" type= "System.Web.HttpForbiddenHandler" validate= "True"/>
<add path= "*.vb" verb= "*" type= "System.Web.HttpForbiddenHandler" validate= "True"/>
<add path= "*.vbproj" verb= "*" type= "System.Web.HttpForbiddenHandler" validate= "True"/>
<add path= "*.webinfo" verb= "*" type= "System.Web.HttpForbiddenHandler" validate= "True"/>
<add path= "*.licx" verb= "*" type= "System.Web.HttpForbiddenHandler" validate= "True"/>
<add path= "*.resx" verb= "*" type= "System.Web.HttpForbiddenHandler" validate= "True"/>
<add path= "*.resources" verb= "*" type= "System.Web.HttpForbiddenHandler" validate= "True"/>
<add path= "*.mdb" verb= "*" type= "System.Web.HttpForbiddenHandler" validate= "True"/>
<add path= "*.vjsproj" verb= "*" type= "System.Web.HttpForbiddenHandler" validate= "True"/>
<add path= "*.java" verb= "*" type= "System.Web.HttpForbiddenHandler" validate= "True"/>
<add path= "*.jsl" verb= "*" type= "System.Web.HttpForbiddenHandler" validate= "True"/>
<add path= "*.ldb" verb= "*" type= "System.Web.HttpForbiddenHandler" validate= "True"/>
<add path= "*.ad" verb= "*" type= "System.Web.HttpForbiddenHandler" validate= "True"/>
<add path= "*.DD" verb= "*" type= "System.Web.HttpForbiddenHandler" validate= "True"/>
<add path= "*.LDD" verb= "*" type= "System.Web.HttpForbiddenHandler" validate= "True"/>
<add path= "*.SD" verb= "*" type= "System.Web.HttpForbiddenHandler" validate= "True"/>
<add path= "*.CD" verb= "*" type= "System.Web.HttpForbiddenHandler" validate= "True"/>
<add path= "*.adprototype" verb= "*" type= "System.Web.HttpForbiddenHandler" validate= "True"/>
<add path= "*.lddprototype" verb= "*" type= "System.Web.HttpForbiddenHandler" validate= "True"/>
<add path= "*.SDM" verb= "*" type= "System.Web.HttpForbiddenHandler" validate= "True"/>
<add path= "*.sdmdocument" verb= "*" type= "System.Web.HttpForbiddenHandler" validate= "True"/>
<add path= "*.mdf" verb= "*" type= "System.Web.HttpForbiddenHandler" validate= "True"/>
<add path= "*.ldf" verb= "*" type= "System.Web.HttpForbiddenHandler" validate= "True"/>
<add path= "*.exclude" verb= "*" type= "System.Web.HttpForbiddenHandler" validate= "True"/>
<add path= "*.refresh" verb= "*" type= "System.Web.HttpForbiddenHandler" validate= "True"/>
<add path= "*" verb= "get,head,post" type= "System.Web.DefaultHttpHandler" validate= "True"/>
<add path= "*" verb= "*" type= "System.Web.HttpMethodNotAllowedHandler" validate= "True"/>


The above code understands that different file types are mapped to different handler at

The above is the default handler processing, of course, after HttpHandler, we can also register their own HttpHandler, write their own httphandler processing procedures, processing different types of files, this will be realized in a moment, I give you a look.

Question: Since the role of HttpHandler is to map files of different types of suffix names to different handlers in the request, does the handler map the processing request at that time in the page life cycle?

A: According to the above 24 events, the mappings in this HttpHandler node are triggered in step 10th. And he maps to different handlers, and the implementations of the methods and classes in those handlers are triggered in step 15th.

besides HttpModule.

HttpHandler is a type of file that maps to the specified handler for the request. And the mapping, and processing, occurs in the event that ASP.net has already specified.

HttpModule, for all request files, maps to the specified handler to process the request, which can occur in any of the events in the request pipeline. That is, which event you subscribe to, this write process occurs in that event, after processing, after you have subscribed to the next event, of course, you can also terminate all events directly run the last event, which means that he can not give HttpHandler opportunity, very cow httpmodule.

the use of HttpHandler

The use of HttpHandler is demonstrated by an anti-theft chain technique.

1. Register First HttpHandler: Register in Web.config

Copy Code code as follows:

<!--map jpg format files to processhandler_test. Customhandler processing. -->
<!--type inside a comma before the namespace plus class name (processhandler_test). Customhandler), followed by the assembly name-->
<add path= "*.jpg" verb= "type=" httphander_test. Customhandler, Processhandler_test "/>

The above registration is to request a JPG format file in the Web site, map the namespace to the Httphander_test class named Customhandler Assembly processhandler_test to process the request.

2. If you want to process the request through HttpHandler, you must implement the interface in the mapped handler IHttpHandler

3. The program code mapped to IS as follows

Copy Code code as follows:

View Code

Namespace Httphander_test
{
public class Customhandler:ihttphandler
{
public void ProcessRequest (HttpContext context)
{
To obtain a file server-side physical path
String FileName = context. Server.MapPath (context. Request.filepath);
If Urlreferrer is empty, a picture with a default hotlinking is displayed
if (context. Request.UrlReferrer.Host = null)
{
Context. Response.ContentType = "Image/gif";
Context. Response.WriteFile ("/error.gif");
}
Else
{
If the urlreferrer does not contain its own site host domain name, a picture with a default ban of hotlinking 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 ();}
}
}
}


Press CTRL + C to copy the code above this simple example is completed, if there is a JPG format file request, rather than in the domain name of this site request, then output a specified error picture to replace the original connection picture.

Summary: HttpHandler's function is much more than that, I hope you can understand that he is a kind of file request processing, but also hope you can understand his request in the pipeline of the event location, so it will help you understand.

The use of HttpModule

Because HttpModule is too powerful function, that is, any request must be registered HttpModule processing procedures, so we use his time must be judged on a variety of requests, that is, to deal with what request, let this request to go that process, do not let him each method, are going to execute. Do not let the program load, not worth the candle.

Using HttpModule is similar to HttpHandler's steps, and HttpModule implements the IHttpModule interface.

       here, his specific case, I will not write, I have written a URL rewrite case, is to use it, you can see. Link overrides

for:  URL

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.