ashx文件的使用小結(jié)
一提到Ashx文件,我們就會(huì)想到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:
<img src="myHttpHander.ashx?id=123" width="20" height="20" />
我們變通以下,發(fā)現(xiàn)其實(shí)除了可以輸出圖片以外,還可以輸出文字:
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
<script src="Handler.ashx"></script>
也可以把.ashx當(dāng)成css文件
<link href="css/Handler.ashx" rel="stylesheet" type="text/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:
<script type="text/javascript" src="TestHandler.ashx" />
當(dāng)你希望從ashx或HttpHandler里訪問(wèn)你的Session時(shí),你必須實(shí)現(xiàn)IReadOnlySessionState接口.
代碼:
using System;
using System.Web;
using System.Web.SessionState;
public class DownloadHandler : IHttpHandler, IReadOnlySessionState
{
public bool IsReusable { get { return true; } }
public void ProcessRequest(HttpContext ctx)
{
ctx.Response.Write(ctx.Session["fred"]);
}
}
其實(shí),學(xué)習(xí)的思路不應(yīng)該這樣,以上除了圖片外,我們都用偏了,為什么用偏了呢,因?yàn)檐浖院?jiǎn)單、實(shí)用為主,我們只是把以上純粹看成可一項(xiàng)技術(shù)而沒(méi)有把它放到軟件的地位去考慮:)
具體的用途,大家可以參考Rewirte.dll (這個(gè)dll,可以使服務(wù)器支持偽靜態(tài)的)
相關(guān)文章
Asp.net?mvc實(shí)現(xiàn)上傳頭像加剪裁功能
這篇文章主要介紹了實(shí)現(xiàn)Asp.net?mvc上傳頭像加剪裁功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2015-08-08.Net WebApi消息攔截器之MessageHandler的示例
這篇文章主要介紹了.Net WebApi消息攔截器之MessageHandler的示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-08-08Asp.Net網(wǎng)站優(yōu)化系列之?dāng)?shù)據(jù)庫(kù)的優(yōu)化措施與索引優(yōu)化方法
索引的作用就類似于書(shū)的目錄,書(shū)的目錄會(huì)按照章節(jié)的順序排列,會(huì)指想某一張的位置。這樣如果在一本數(shù)百頁(yè)的書(shū)里面查找某個(gè)章節(jié)位置的時(shí)候,我們就可以只掃描書(shū)的目錄,掃描的范圍縮小了n倍,查詢的效率自然就提高了。2010-06-06阿里云上從ASP.NET線程角度對(duì)“黑色30秒”問(wèn)題的全新分析
在這篇博文中,我們拋開(kāi)對(duì)阿里云的懷疑,完全從ASP.NET的角度進(jìn)行分析,看能不能找到針對(duì)問(wèn)題現(xiàn)象的更合理的解釋2015-09-09asp.net基于session實(shí)現(xiàn)購(gòu)物車的方法
這篇文章主要介紹了asp.net基于session實(shí)現(xiàn)購(gòu)物車的方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了asp.net使用session存儲(chǔ)臨時(shí)數(shù)據(jù)實(shí)現(xiàn)購(gòu)物車功能的相關(guān)技巧,需要的朋友可以參考下2015-11-11MVC+EasyUI+三層新聞網(wǎng)站建立 建站準(zhǔn)備工作(一)
這篇文章主要為大家詳細(xì)介紹了MVC+EasyUI+三層新聞網(wǎng)站建立的第一篇,建站的準(zhǔn)備工作,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07