The things that IHttpHandler

Source: Internet
Author: User
Tags httpcontext

Write in front

From the home company leave, rest, idle bored, think it is necessary to ihttphanlder content, do a summary. Found in the process of writing the demo, always feel a little rusty, the project rarely use a custom class to implement the interface. Of course, the exception is general processing procedures.

The things that IHttpModule

IHttpHandler

In IHttpModule This article, there is a schematic of the ASP. NET life cycle. Forget the can, turn to the article, and then familiar.

When an HTTP request arrives at HttpModule, the entire ASP. NET Framework system does not do any processing of this HTTP request, that is, at this point, for HTTP requests, HttpModule is the "Only way" for an HTTP request, It is possible to append some required information to this HTTP request message before the HTTP request is passed to the Real Request Processing Center (HttpHandler), or to do some extra work on the intercepted HTTP request message. Or, in some cases, simply terminating an HTTP request that satisfies some condition, which can act as a filter.

HttpHandlerhttphttphandlerasp.net frameworkhttphttpmodule

IHttpHandlerdefines some of the system conventions that are necessary to implement the processing of an HTTP request. HttpHandler is different from HttpModule, once you define your own HttpHandler class, the relationship between the HttpHandler of the system will be "overwrite".

When an HTTP request is passed to the HttpHandler container by the same HttpModule container, the ASP. NET Framework calls HttpHandler's ProcessRequest member method to really handle the HTTP request. As an example of an ASPX page, it is here that an ASPX page is parsed by the system and continues the processing of the completed results through HttpModule until it reaches the client.

For ASPX pages, the ASP. NET Framework is handed over by default to the System.Web.UI.PageHandlerFactory httphandlerfactory. So-called a httphandlerfactory, called a httphandlerfactory, refers to when an HTTP request arrives at this HttpHandler factory, Httphandlerfactory will provide a HttpHandler container for this HttpHandler container to handle this HTTP request.

An HTTP request is ultimately handled by the ProcessRequest method in a HttpHandler container.

Customizing a class that processes requests

By implementingIHttpHandlerInterface can create customHTTPHandler, the interface contains only two methods. by callingIsReusable,IHttpHandlerFactoryYou can query the handler to determine whether you can service multiple requests with the same instance.ProcessRequestMethod willHttpContextrequestresponsehttphandlersessionirequiressessionstate

1 usingsystem.web;2 usingSystem.Web.SessionState;3 namespaceMyhttphandler4 {5     /// <summary>6     ///Custom Httphanlder7     /// </summary>8      Public classmyhttphandler:ihttphandler,irequiressessionstate9     {Ten          Public BOOLisreusable One         { A             Get{return true; } -         } -  the          Public voidProcessRequest (HttpContext context) -         { -Context. session["User"]="Wolfy"; -Context. Response.Write (context. session["User"]. ToString ()); +Context. Response.Write ("HttpHandler"); -         } +     } A}

Registering the custom Httphanlder in Web. config

<Configuration>  <system.web>    <compilationDebug= "true"targetframework= "4.5" />    <HttpRuntimetargetframework= "4.5" />     </system.web>  <system.webserver>    <handlers>      <Addname= "Yhandler"Path= "*.aspx"verb="*"type= "Myhttphandler.myhttphandler,myhttphandler"/>    </handlers>  </system.webserver></Configuration>

Name: Feel free to

Path: Specifies which pages are requested to use the custom Htpphanlder, and wildcard characters are supported, as in the example above *.aspx.

Type: The full name of the class (namespace + class name), assembly name.

Browse ASPX pages

Ihttphanlder Factory

The ASP. NET Framework actually does not directly target the related page resource HTTP request to an internal default IHttpHandler container, but to its internal default IHttpHandler factory. The role of the IHttpHandler plant is to dispatch and manage IHttpHandler containers.

The IHttpHandlerFactory interface consists of two methods. GetHandler returns an instance of the class that implements the IHttpHandler interface, Releasehandler enables the factory to reuse an existing handler instance.

namespacemyhttphandler{/// <summary>    ///Handler processing Plant/// </summary>     Public classHandlerfactory:ihttphandlerfactory { PublicIHttpHandler GetHandler (HttpContext context,stringRequestType,stringUrlstringpathtranslated) {PageHandlerFactory Factory= (pagehandlerfactory) activator.createinstance (typeof(PageHandlerFactory),true); IHttpHandler Handler=Factory.            GetHandler (context, RequestType, url, pathtranslated); Page Page= Handler asPage; if(Page! =NULL) {page. Init+=NewEventHandler (Page_Init); }            returnhandler; }        Private voidPage_Init (Objectsender, EventArgs e) {HttpContext.Current.Response.Write ("IHttpHandlerFactory"); }         Public voidReleasehandler (IHttpHandler handler) {}}}

Registering a factory in Web. config

1 <Configuration>2   <system.web>3     <compilationDebug= "true"targetframework= "4.5" />4     <HttpRuntimetargetframework= "4.5" />5    6   </system.web>7   <system.webserver>8     <handlers>9       <!--<add name= "Yhandler" path= "*.aspx" verb= "*" type= "Myhttphandler.myhttphandler,myhttphandler"/> -Ten       <Addname= "Handler1"Path= "*.aspx"verb="*"type= "Myhttphandler.handlerfactory,myhttphandler"/> One     </handlers> A   </system.webserver> - </Configuration>

Browse

This way, can achieve unified authentication, in the Page_Init method, judge the session, determine whether the user landed.

Summary

Through the implementation of the IHttpHandler interface, the request can be made special processing, such as the implementation of the anti-theft chain, verification code implementation methods.
Reference articles

Basic concept--httphandler of 1.1-point learning ASP.

The things that IHttpHandler

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.