The ashx file is used to write web handler .. Ashx must contain isreusable. As shown in the following example .}. The advantage of ashx over. aspx is that there is no need to add more HTML to pay attention to the Web application in vs2005.ProgramThe generic handler item in the Project template finds that it is A. ashx file, in fact it is an httphandler. Exploitation. the ashx file is a better method. This file is similar. the aspx file, which can be used to call the httphandler class, thus eliminating the need for common. the control parsing and page processing process of the aspx page. Then, in the same directory, use solution Resource Manager, use "add" --> "add class", and enter "textbuilder. ashx. cs" in the class file name ". Run the IE test and enter the. ashx address.
What is httphandler?
Httphandler is the real processing center of HTTP requests. It is precisely in this httphandler container that ASP. NET Framework actually requests the client.ServerThe page is compiled and executed, and the processed information is appended to the HTTP request information stream and returned to httpmodule again.
What is ihttphandler?
Ihttphandler defines some system conventions that must be implemented to process an HTTP request. Different from httpmodule, httphandler defines its own httphandler class, and 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 httphandlerProThe cessrequest member method is used 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 sets the htTPCThe ontext instance is used 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
{
///
/// Objective: to implement a simple custom httphandler container
/// Author: wenye
// Contact: stwyhm@cnblogs.com
///
Public class myfirsthandler: ihttphandler, irequiressessionstate
{
# Region ihttphandler Member
Public bool isreusable
{
Get {return true ;}
}
Public void processrequest (httpcontext context)
{
Context. response. Write ("
Hello httphandler
");
Context. session ["test"] ="TestCall session in httphandler container ";
Context. response. Write (context. session ["test"]);
}
# Endregion
}
}
Add the following configuration to Web. config:
<Span> httphandlers>
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:
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 ("the factory cannot be of the 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 ("
Information from handler1.
");
}
# Endregion
}
Public class handler2: ihttphandler
{
# Region ihttphandler Member
Public bool isreusable
{
Get {return true ;}
}
Public void processrequest (httpcontext context)
{
Context. response. Write ("
Information from handler2.
");
}
# Endregion
}
}