Python语言技术文档

微信小程序技术文档

php语言技术文档

jsp语言技术文档

asp语言技术文档

C#/.NET语言技术文档

html5/css技术文档

javascript

点击排行

您现在的位置:首页 > 技术文档 > C#/.NET技巧

ashx文件的使用小结

来源:中文源码网    浏览:444 次    日期:2024-04-19 06:30:49
【下载文档:  ashx文件的使用小结.txt 】


ashx文件的使用小结
一提到Ashx文件,我们就会想到http handler以及图片加载(在之前我们一般使用ASPX或者Webservice去做),一般做法如下:
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:
我们变通以下,发现其实除了可以输出图片以外,还可以输出文字: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也可以把.ashx当成css文件
xml文件orderDoc.load("Handler.ashx");
还可以嵌入文字: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:

相关内容