When we mention the ashx file, we will think of HTTP handler and image loading (we used aspx or WebService before). The general practice is as follows:
Handler. ashx:
<% @ Webhandler Language = "C #" class = "handler" %>
Using system;
Using system. IO;
Using system. Web;
Public class handler: ihttphandler {
Public bool isreusable {
Get {
Return true;
}
}
Public void processrequest (httpcontext context ){
Context. response. contenttype = "image/JPEG ";
Context. response. cache. setcacheability (httpcacheability. Public );
Context. response. bufferoutput = false;
Photosize size;
Switch (context. Request. querystring ["size"]) {
Case "S ":
Size = photosize. Small;
Break;
Case "M ":
Size = photosize. medium;
Break;
Case "L ":
Size = photosize. Large;
Break;
Default:
Size = photosize. Original;
Break;
}
Int32 id =-1;
Stream stream = NULL;
If (context. Request. querystring ["photoid"]! = NULL & context. Request. querystring ["photoid"]! = ""){
Id = convert. toint32 (context. Request. querystring ["photoid"]);
Stream = photomanager. getphoto (ID, size );
} Else {
Id = convert. toint32 (context. Request. querystring ["albumid"]);
Stream = photomanager. getfirstphoto (ID, size );
}
If (Stream = NULL) stream = photomanager. getphoto (size );
Const int buffersize = 1024*16;
Byte [] buffer = new byte [buffersize];
Int COUNT = stream. Read (buffer, 0, buffersize );
While (count> 0 ){
Context. response. outputstream. Write (buffer, 0, count );
Count = stream. Read (buffer, 0, buffersize );
}
}
}
*. Aspx:
We can work with the following changes and find that in addition to outputting images, we can also output text:
Handler. ashx:
<% @ Webhandler Language = "C #" class = "handler" %>
Using system;
Using system. Web;
Public class handler: ihttphandler {
Public void processrequest (httpcontext context ){
Context. response. contenttype = "text/plain ";
Context. response. Write ("alert ('Hi ')");
}
Public bool isreusable {
Get {
Return false;
}
}
}
*. Aspx:
Alert pop-up
<SCRIPT src = "handler. ashx"> </SCRIPT>
You can also use. ashx as a CSS file.
<Link href = "CSS/handler. ashx" rel = "stylesheet" type = "text/CSS">
XML file
Orderdoc. Load ("handler. ashx ");
You can also embed text:
Handler. ashx:
<% @ Webhandler Language = "C #" class = "testhandler" %>
Using system;
Using system. Web;
Public class testhandler: ihttphandler {
Public void processrequest (httpcontext context ){
Context. response. contenttype = "text/plain ";
Context. response. Write ("document. Write (\" Hello world \");");
}
Public bool isreusable {
Get {
Return false;
}
}
}
*. Aspx:
<SCRIPT type = "text/JavaScript" src = "testhandler. ashx"/>
When you want to access your session from ashx or httphandler, you must implement the ireadonlysessionstate interface.
Code:
Using system;
Using system. Web;
Using system. Web. sessionstate;
Public class downloadhandler: ihttphandler, ireadonlysessionstate
{
Public bool isreusable {get {return true ;}}
Public void processrequest (httpcontext CTX)
{
CTX. response. Write (CTX. session ["Fred"]);
}
}