asp.net ashx文件的使用[转]
一提到Ashx文件,我们就会想到http handler以及图片加载(在之前我们一般使用ASPX或者Webservice去做),一般做法如下:
Handler.ashx:
1 <%@ WebHandler Language="C#" Class="Handler" %> 2 using System; 3 using System.IO; 4 using System.Web; 5 public class Handler : IHttpHandler 6 { 7 public bool IsReusable { 8 get { 9 return true; 10 } 11 } 12 public void ProcessRequest (HttpContext context) { 13 context.Response.ContentType = "image/jpeg"; 14 context.Response.Cache.SetCacheability(HttpCacheability.Public); 15 context.Response.BufferOutput = false; 16 PhotoSize size; 17 switch (context.Request.QueryString["Size"]) { 18 case "S": 19 size = PhotoSize.Small; 20 break; 21 case "M": 22 size = PhotoSize.Medium; 23 break; 24 case "L": 25 size = PhotoSize.Large; 26 break; 27 default: 28 size = PhotoSize.Original; 29 break; 30 } 31 Int32 id = -1; 32 Stream stream = null; 33 if (context.Request.QueryString["PhotoID"] != null && context.Request.QueryString["PhotoID"] != "") { 34 id = Convert.ToInt32(context.Request.QueryString["PhotoID"]); 35 stream = PhotoManager.GetPhoto(id, size); 36 } else { 37 id = Convert.ToInt32(context.Request.QueryString["AlbumID"]); 38 stream = PhotoManager.GetFirstPhoto(id, size); 39 } 40 if (stream == null) stream = PhotoManager.GetPhoto(size); 41 const int buffersize = 1024 * 16; 42 byte[] buffer = new byte[buffersize]; 43 int count = stream.Read(buffer, 0, buffersize); 44 while (count > 0) { 45 context.Response.OutputStream.Write(buffer, 0, count); 46 count = stream.Read(buffer, 0, buffersize); 47 } 48 } 49 }
*.aspx调用:
<img src="myHttpHander.ashx?id=123" width="20" height="20" />
我们变通以下,发现其实除了可以输出图片以外,还可以输出文字
Handler.ashx:
1 <%@ WebHandler Language="C#" Class="Handler" %> 2 using System; 3 using System.Web; 4 public class Handler : IHttpHandler { 5 6 public void ProcessRequest (HttpContext context) { 7 context.Response.ContentType = "text/plain"; 8 context.Response.Write("alert('hi')"); 9 } 10 11 public bool IsReusable { 12 get { 13 return false; 14 } 15 } 16 }
*.aspx调用:
<script src="Handler.ashx"></script>
也可以把.ashx当成css文件
<link href="css/Handler.ashx" rel="stylesheet" type="text/css">
xml文件
orderDoc.load("Handler.ashx");
还可以嵌入文字
Handler.ashx:
1 <%@ WebHandler Language="C#" Class="TestHandler" %> 2 using System; 3 using System.Web; 4 public class TestHandler : IHttpHandler { 5 6 public void ProcessRequest (HttpContext context) { 7 context.Response.ContentType = "text/plain"; 8 context.Response.Write("document.write(\"Hello World\");"); 9 } 10 11 12 public bool IsReusable { 13 get { 14 return false; 15 } 16 } 17 }
*.aspx调用:
<script type="text/javascript" src="TestHandler.ashx" />
当你希望从ashx或HttpHandler里访问你的Session时,你必须实现IReadOnlySessionState接口
1 using System; 2 using System.Web; 3 using System.Web.SessionState; 4 5 public class DownloadHandler : IHttpHandler, IReadOnlySessionState 6 { 7 public bool IsReusable { get { return true; } } 8 9 public void ProcessRequest(HttpContext ctx) 10 { 11 ctx.Response.Write(ctx.Session["fred"]); 12 } 13 }
其实,学习的思路不应该这样,以上除了图片外,我们都用偏了,为什么用偏了呢,因为软件以简单、实用为主,我们只是把以上纯粹看成可一项技术而没有把它放到软件的地位去考虑:)
具体的用途,大家可以参考Rewirte.dll (这个dll,可以使服务器支持伪静态)!

浙公网安备 33010602011771号