ashx文件的使用小結(jié)
一提到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:
<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里訪問你的Session時,你必須實(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)檐浖院唵?、?shí)用為主,我們只是把以上純粹看成可一項(xiàng)技術(shù)而沒有把它放到軟件的地位去考慮:)
具體的用途,大家可以參考Rewirte.dll (這個dll,可以使服務(wù)器支持偽靜態(tài)的)
相關(guān)文章
.NET?Core實(shí)現(xiàn)企業(yè)微信消息推送
這篇文章介紹了.NET?Core實(shí)現(xiàn)企業(yè)微信消息推送的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-06-06
.NET下為百度文本編輯器UEditor增加圖片刪除功能示例
今天下載了目前最新版1.2.5為版本看更新記錄,主要是對表格做個修改,我下載用上,上傳圖片的刪除功能給取消了,下面與大家分享下增加圖片刪除功能示例2013-05-05
SignalR中豐富多彩的消息推送方式的實(shí)現(xiàn)代碼
這篇文章主要介紹了SignalR中豐富多彩的消息推送方式的實(shí)現(xiàn)代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04

