ASP. NET provides low-level request/response APIs, allowing developers to use the. NET Framework class to provide services for incoming HTTP requests. Therefore, developers need to create classes that support the system. Web. ihttphandler interface and processrequest () method. The processing program is usually useful when processing HTTP requests without services abstracted by a high-level page framework. Common uses of handlers include filters and CGI-like applications, especially those that return binary data.
Each HTTP request received by ASP. NET is finally processed by a specific instance of the ihttphandler class. Ihttphandlerfactory provides the structure for actual resolution of ihttphandler instance URL requests. In addition to the default ihttphandlerfactory class provided by ASP. NET, developers can also choose to create and register factories to support a large number of Request Parsing and activation schemes.
From this text, we can see that when the ASPX page is not involved. Net Framework provides advanced interface technologies (such as data caching, status persistence, web form Control Reference, etc.), and does not output complex HTML text to the client, in particular, you can use one to only return binary data (slice, sound, etc.) to the client. CS application file (This article uses the C # language, if it is VB or JScript ,......) The application must have a class that implements the system. Web. ihttphandler interface and processrequest () method. A simple example is as follows:
[WebService (namespace = "http://tempuri.org/")]
[Webservicebinding (conformsto = wsiprofiles. basicprofile1_1)]
Public class imagehander: ihttphandler, ireadonlysessionstate
{
Public void processrequest (httpcontext context)
{
String processid = context. Request. querystring ["processid"];
If (! String. isnullorempty (processid ))
{
Array arr = (array) Context. session [processid];
Context. clearerror ();
Context. response. expires = 0;
Context. response. Buffer = true;
Context. response. Clear ();
Memorystream memstream = new memorystream (byte []) Arr );
Memstream. writeto (context. response. outputstream );
Memstream. Close ();
Context. response. contenttype = "image/jpg ";
Context. response. statuscode = 200;
Context. response. End ();
}
}
Public bool isreusable
{
Get
{
Return false;
}
}
}
Call on the page:
Int I = 1;
Byte [] picturedata = getpicturedata (server. mappath ("images/factorymethod.jpg "));
Session [I. tostring ()] = picturedata;
This. image1.imageurl = "imagehander. ashx? Chartid = "+ I;
The getpicturedata method is as follows:
Code
Public byte [] getpicturedata (string ImagePath)
{
Filestream FS = new filestream (ImagePath, filemode. Open); // It Can Be Another overload method.
Byte [] bydata = new byte [fs. Length];
FS. Read (bydata, 0, bydata. Length );
FS. Close ();
Return bydata;
}
The final effect is displayed in factorymethod.jpg under imageson the page.
Another example:
Namespace webapplication1
{
/** // <Summary>
/// $ Codebehindclassname $ abstract description
/// </Summary>
[WebService (namespace = "http://tempuri.org/")]
[Webservicebinding (conformsto = wsiprofiles. basicprofile1_1)]
/** // The class must implement the ihttphandler interface. If the program accesses the session state (Session), The irequiressessionstate interface must be implemented (the flag interface that does not contain any methods ). */
Public class dllhander: ihttphandler
{
Public void processrequest (httpcontext context)
{
Httpresponse res = context. response;
Res. Write ("<HTML> <body> ");
Res. Write ("Res. Write ("this page is directly processed by DLL ");
Res. Write ("}
Public bool isreusable
{
Get
{
Return true;
}
}
}
}
Add aspx-> DLL ing to the configuration file web. config. After adding the map, Web. config should be like this:
<Httphandlers>
<Add verb = "*" Path = "index. aspx" type = "webapplication1.dllhander, webapplication1"/>
</Httphandlers>
Now when the browser accesses this address, http: // localhost/webapplication1/index. aspx actually calls webapplication. the processrequest method of the dllhander class in dll should be able to see a simple page during browsing.
Finally, I hope my friends can raise questions and solve them together. Thank you!
Charles Chen
MSN: Charles.C.Chen@newegg.com
Email: gotosunny@msn.com