1. httphandler
Instance 1: generate a dynamic image to output the Client IP address, operating system type, and browser type.
<% @ Webhandler Language = " C # " Class = " Visitor " %> Using System; Using System. Web; Public Class Visitor: ihttphandler { Public Void Processrequest (httpcontext context) {context. response. contenttype = " Image/JPEG " ; Using (System. Drawing. Bitmap bitimage = New System. Drawing. Bitmap ( 330 , 300 )){ // Set canvas Using (System. Drawing. Graphics G = System. Drawing. Graphics. fromimage (bitimage )){ // IP G. drawstring ( " IP: " + Context. Request. userhostaddress, New System. Drawing. Font ( " " , 20 ), System. Drawing. Brushes. Red, New System. Drawing. pointf ( 0 , 0 )); // Operating System G. drawstring ( " Operating System: " + Context. Request. browser. platform, New System. Drawing. Font ( " " , 20 ), System. Drawing. Brushes. Red,New System. Drawing. pointf ( 0 , 50 )); // Browser G. drawstring ( " Browser: " + Context. Request. browser. type, New System. Drawing. Font ( " " , 20 ), System. Drawing. Brushes. Red, New System. Drawing. pointf ( 0 , 100 ));} // Save to output stream Bitimage. Save (context. response. outputstream, system. Drawing. imaging. imageformat. JPEG );}} Public Bool Isreusable { Get { Return False ;}}}
Instance 2: Click Download to connect. The user attachment is saved.
HtmlCode: <A href = "dowload. ashx"> download </a>
<% @ Webhandler Language = " C # " Class = " Dowload " %> Using System; Using System. Web; Public Class Dowload: ihttphandler { Public Void Processrequest (httpcontext context ){ String Filename = httputility. urlencode ( " Tragedy .jpg " ); Context. response. contenttype = " Image/JPEG " ; // Open the header and set its value in the attachment dialog box. Context. response. addheader ( " Content-Disposition " , " Attachment: filename = " + Filename); context. response. writefile ( " Apple .jpg " );} Public Bool Isreusable { Get { Return False ;}}}