Custom HTTP ProcessingProgramThat is, the HTTP handler must implement the interface system. Web. ihttphandler.
When implementing the ihttphandler interface, you must do two things: Setting the isreusable attribute and implementing the processrequest () method.
Create generic handler.
<% @ 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 ){
// Set up the response settings
Context. response. contenttype = "image/JPEG ";
Context. response. cache. setcacheability (httpcacheability. Public );
Context. response. bufferoutput = false;
// Setup the size parameter
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;
}
// Setup the photoid Parameter
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 );
}
// Get the photo from the database, if nothing is returned, get the default "Placeholder" photo
If (Stream = NULL) stream = photomanager. getphoto (size );
// Write image stream to the response stream
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 );
}
}
}
Public static stream getphoto (INT photoid, photosize size ){
Using (sqlconnection connection = new sqlconnection (configurationmanager. connectionstrings ["personal"]. connectionstring )){
Using (sqlcommand command = new sqlcommand ("getphoto", connection )){
Command. commandtype = commandtype. storedprocedure;
Command. Parameters. Add (New sqlparameter ("@ photoid", photoid ));
Command. Parameters. Add (New sqlparameter ("@ size", (INT) size ));
Bool filter =! (Httpcontext. Current. User. isinrole ("friends") | httpcontext. Current. User. isinrole ("Administrators "));
Command. Parameters. Add (New sqlparameter ("@ ispublic", filter ));
Connection. open ();
Object result = command. executescalar ();
Try {
Return new memorystream (byte []) result );
} Catch {
Return NULL;
}
}
}
}
Public static stream getfirstphoto (INT albumid, photosize size ){
Using (sqlconnection connection = new sqlconnection (configurationmanager. connectionstrings ["personal"]. connectionstring )){
Using (sqlcommand command = new sqlcommand ("getfirstphoto", connection )){
Command. commandtype = commandtype. storedprocedure;
Command. Parameters. Add (New sqlparameter ("@ albumid", albumid ));
Command. Parameters. Add (New sqlparameter ("@ size", (INT) size ));
Bool filter =! (Httpcontext. Current. User. isinrole ("friends") | httpcontext. Current. User. isinrole ("Administrators "));
Command. Parameters. Add (New sqlparameter ("@ ispublic", filter ));
Connection. open ();
Object result = command. executescalar ();
Try {
Return new memorystream (byte []) result );
} Catch {
Return NULL;
}
}
}
}