Detailed description of Httphandler operations in ASP. NET notes

Source: Internet
Author: User

1. httphandler

Instance 1: generate a dynamic image to output the Client IP address, operating system type, and browser type.

Copy codeThe Code is as follows: <% @ 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 the 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 it to the 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.

Html code: <a href = "dowload. ashx"> download </a>

Copy codeThe Code is as follows: <% @ 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 of the message in the attachment dialog box and set its value
Context. Response. AddHeader ("Content-Disposition", "attachment: filename =" + fileName );
Context. Response. WriteFile ("Apple .jpg ");
}

Public bool IsReusable {
Get {
Return false;
}
}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.