ASP.NET筆記之 Httphandler的操作詳解
1、httphandler

實(shí)例1:通過生成一張動(dòng)態(tài)圖片輸出客戶端的IP地址、操作系統(tǒng)類型、瀏覽器類型
<%@ 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))
{
//設(shè)置畫布
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));
//操作系統(tǒng)
g.DrawString("操作系統(tǒng):" + context.Request.Browser.Platform, new System.Drawing.Font("宋體", 20), System.Drawing.Brushes.Red, new System.Drawing.PointF(0, 50));
//瀏覽器
g.DrawString("瀏覽器:" + context.Request.Browser.Type, new System.Drawing.Font("宋體", 20), System.Drawing.Brushes.Red, new System.Drawing.PointF(0, 100));
}
//保存到輸出流中
bitImage.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
public bool IsReusable {
get {
return false;
}
}
}
實(shí)例2:通過“下載”連接,彈出用戶附件保存
html代碼:<a href="dowload.ashx">下載</a>
<%@ WebHandler Language="C#" Class="dowload" %>
using System;
using System.Web;
public class dowload : IHttpHandler {
public void ProcessRequest(HttpContext context)
{
string fileName = HttpUtility.UrlEncode("悲劇.jpg");
context.Response.ContentType = "image/JPEG";
//打開附件對話框 報(bào)文頭header和設(shè)置它的值
context.Response.AddHeader("Content-Disposition", "attachment:filename=" + fileName);
context.Response.WriteFile("蘋果.jpg");
}
public bool IsReusable {
get {
return false;
}
}
}
相關(guān)文章
ASP.NET Core讀取Request.Body的正確方法
相信大家在使用ASP.NET Core進(jìn)行開發(fā)的時(shí)候,肯定會(huì)涉及到讀取Request.Body的場景,畢竟我們大部分的POST請求都是將數(shù)據(jù)存放到Http的Body當(dāng)中,本文就介紹一下ASP.NET Core讀取Request.Body,感興趣的可以了解一下2021-05-05
關(guān)于.NET Framework中的設(shè)計(jì)模式--應(yīng)用策略模式為List排序
本篇文章,小編將為大家介紹關(guān)于.NET Framework中的設(shè)計(jì)模式--應(yīng)用策略模式為List排序,有需要的朋友可以參考一下2013-04-04
利用Asp.Net Core的MiddleWare思想如何處理復(fù)雜業(yè)務(wù)流程詳解
這篇文章主要給大家介紹了關(guān)于利用Asp.Net Core的MiddleWare思想如何處理復(fù)雜業(yè)務(wù)流程的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起看看吧2018-08-08
ASP.NET Core奇淫技巧之動(dòng)態(tài)WebApi的實(shí)現(xiàn)
這篇文章主要介紹了ASP.NET Core奇淫技巧之動(dòng)態(tài)WebApi的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
運(yùn)用.net core中實(shí)例講解RabbitMQ
RabbitMQ是實(shí)現(xiàn)了高級消息隊(duì)列協(xié)議(AMQP)的開源消息代理軟件(亦稱面向消息的中間件),本文詳細(xì)講解了RabbitMQ以及運(yùn)用.net core中實(shí)例講解其6中模式,感興趣的小伙伴一起來學(xué)習(xí)吧2021-09-09

