IHttpHandler is the interface for ASP. NET to process actual operations. As defined in MSDN, a custom HTTP handler is used to synchronously process HTTP Web requests. (Note: It is clearly written here that the IHttpAsyncHandler interface program is used to synchronize HTTP requests asynchronously ). The IsReusable attribute is used to obtain whether the current IHttpHandler instance is available. It is generally set to True. A method ProcessRequest (HttpContext context) is used for actual operations.
Custom IHttpHandler
Three steps are required to use a custom IHttpHandler handler.
1) define a class that implements the IHttpHandler interface.
2) register this class in the Web. config file
3) execute the corresponding HttpRequst
The following is an example of an image anti-theft link:
Public class LinkProtectHandler: IHttpHandler {# region IHttpHandler member public bool IsReusable {get {return true ;}} public void ProcessRequest (HttpContext context) {// specific operation // obtain the physical path of the File Server string fileName = context. server. mapPath (context. request. filePath); // if UrlReferrer is empty, a default anti-leeching image if (context. request. urlReferrer. host = null) {context. response. contentType = "image/JPEG"; context. response. writeFile ("/LinkProtect.jpg");} else {// if UrlReferrer does not contain the host domain name of your site, a default anti-leech image if (context. request. urlReferrer. host. indexOf ("mydomain.com")> 0) {context. response. contentType = "image/JPEG"; context. response. writeFile (fileName);} else {context. response. contentType = "image/JPEG"; context. response. writeFile ("/LinkProtect.jpg") ;}}# endregion}
The Web. Config file is registered as follows:
Custom IHttpHandlerFactory
IHttpHandlerFactory is defined in MSDN as follows: defines the Protocol that the class factory must implement to create a new IHttpHandler object. It contains two interface methods, GetHandler (): return the instance of the class implementing the IHttp interface. ReleaseHandler (): The factory can reuse existing processing program instances. This interface is used to place many methods that implement the IHttpHandler interface in the Factory to mainly register this class during Web. config registration. When using this interface, the Factory will determine which IHttpHandler instance to use.
Public class CustomerHttpHandlerFactory: IHttpHandlerFactory {# region IHttpHandlerFactory member public IHttpHandler GetHandler (HttpContext context, string requestType, string url, string pathTranslated) {// obtain the physical path of the File Server string path = context. request. physicalPath; // get the filename suffix string exstention = Path. getExtension (path); // determines whether to submit the request to the corresponding handler if (exstention = ". rss ") {return new RssHandler ();} else if (exstention = ". atmo ") {return new ATMOHandler () ;}else {return null ;}} public void ReleaseHandler (IHttpHandler handler) {}# endregion}
Use the following definitions in Web. Config:
IHttpAsyncHandler asynchronous Interface
IHttpAsyncHandler describes this in MSDN: defines the protocol that must be implemented by the HTTP asynchronous processing program object. It contains three methods:
BeginProcessRequest: starts asynchronous calls to HTTP processing programs. EndProcessRequest: provides an End Method for asynchronous processing when the process ends. ProcessRequest: enables HTTP Web request processing by implementing the custom HttpHandler interface of the IHttpHandler interface. (Inherited from IHttpHandler .) And an IsReusable attribute.