Overview
Httphandler is the real processing center of HTTP requests. It is precisely in this httphandler container that ASP. net Framework is really compiling and executing the Server Page requested by the client, and attaching the processed information to the HTTP request information stream to return to httpmodule again.
What is ihttphandler?
Ihttphandler defines some system conventions that must be implemented to process an HTTP request. Httphandler is different from httpmodule. Once you define your own httphandler class, its relationship to the system's httphandler will be "overwritten.
How does ihttphandler handle HTTP requests?
When an HTTP request is passed to the httphandler container by the same httpmodule container, ASP. NET Framework calls the processrequest member method of httphandler to process the HTTP request. Take An ASPX page as an example. It is in this case that An ASPX page is parsed by the system, and the processed result is passed through httpmodule until it reaches the client.
For aspx pages, ASP. NET Framework is handed over to the httphandlerfactory system. Web. UI. pagehandlerfactory by default. An httphandlerfactory or httphandlerfactory indicates that when an HTTP request arrives at the httphandler factory, httphandlerfactory provides an httphandler container to process the HTTP request.
An HTTP request is finally sent to the processrequest method in an httphandler container for processing.
Figure 1: processrequest Method
A simple httphandler container
You can create a custom HTTP handler through the ihttphandler interface, which contains only two methods. By calling isreusable, ihttphandlerfactory can query the processing program to determine whether the same instance can be used to provide services for multiple requests. The processrequest method uses an httpcontext instance as a parameter so that it can access internal objects in the request and response. To access the session in an httphandler container, you must implement the irequiressessionstate interface, which is only a tag interface and has no method.
Example 1:
Using system; using system. collections. generic; using system. text; using system. web; using system. web. sessionstate; namespace myhandler {// <summary> // objective: to implement a simple custom httphandler container /// Author: wenye // contact: stwyhm@cnblogs.com /// </Summary> public class myfirsthandler: ihttphandler, irequiressessionstate {# region ihttphandler member public bool isreusable {get {return true ;}} public void processrequest (httpcontext context) {context. response. write ("
Add the following configuration to Web. config:
Ihttphandler Factory
ASP. NET Framework does not directly locate the relevant page resource HTTP request to an internal default ihttphandler container, but to its internal default ihttphandler factory. The ihttphandler factory is used to schedule and manage the ihttphandler container.
The ihttphandlerfactory Interface contains two methods. Gethandler returns an instance of the class implementing the ihttphandler interface. releasehandler allows the factory to reuse existing handler instances.
Example 2:
Using system; using system. collections. generic; using system. text; using system. web; namespace myhandler {public class myhandlerfactory: ihttphandlerfactory {# region ihttphandlerfactory member public ihttphandler gethandler (httpcontext context, string requesttype, string URL, string pathtranslated) {string fname = URL. substring (URL. indexof ('/') + 1); While (fname. indexof ('/')! =-1) fname = fname. substring (fname. indexof ('/') + 1); string cname = fname. substring (0, fname. indexof ('. '); string classname = "myhandler. "+ cname; object H = NULL; try {// use the dynamic reflection mechanism to create the corresponding ihttphandler implementation class. H = activator. createinstance (type. GetType (classname);} catch (exception e) {Throw new httpexception ("factory cannot be type" + cname + "to create an instance. ", E);} return (ihttphandler) h;} public void releasehandler (ihttphandler handler) {}# endregion} public class handler1: ihttphandler {# region ihttphandler member public bool isreusable {get {return true ;}} public void processrequest (httpcontext context) {context. response. write ("<HTML> <body>
Reproduced on:
Http://www.cnblogs.com/stwyhm/archive/2006/08/09/471765.html