ashx文件的几种使用
来源:互联网 发布:黑马程序员培训怎么样 编辑:程序博客网 时间:2024/05/13 08:31
一提到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" />
我们变通以下,发现其实除了可以输出图片以外,还可以输出文字:
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当成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" />
当你希望从ashx或HttpHandler里访问你的Session时,你必须实现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"]);
}
}
其实,学习的思路不应该这样,以上除了图片外,我们都用偏了,为什么用偏了呢,因为软件以简单、实用为主,我们只是把以上纯粹看成可一项技术而没有把它放到软件的地位去考虑:)
具体的用途,大家可以参考Rewirte.dll (这个dll,可以使服务器支持伪静态的)!
- ashx文件的几种使用
- ashx文件的使用
- ashx文件的使用
- ashx文件的使用
- ashx文件的使用实例
- ashx文件的使用小结
- ashx文件使用
- ashx文件使用
- ashx 文件的运用
- ashx 文件的运用
- 创建安全的ashx文件,ashx编译
- 创建安全的ashx文件,ashx编译
- 关于ashx文件的使用的一些个人见解
- ashx文件使用的基类的实现
- ashx文件中使用Session
- ashx文件中使用session
- 关于ashx文件的点点滴滴
- C#中.ascx文件的使用以及.ashx文件的使用
- 再谈使用UrlRewrite时修改form的action属性问题
- 解决framework层修改导致无法开机
- 迷宫算法实现(php)
- hdu 2222
- 中缀,前缀,后缀表达式
- ashx文件的几种使用
- poj 1151.Atlantis
- GCC attribute mechanism
- 做为一个社会的人,不是靠能写多少行代码,代码多么优雅水平多么高来衡量身家的。
- Do We Teach the Right Algorithm Design Techniques ?
- C++类模板 实现双向循环链表的基本算法 《数据结构》(C++版 北京科海)中摘抄
- javascript的HashMap
- Android 通过字符串来获取R下面资源的ID 值
- 动态生成多条插入语句如:insert into MyStudents values(“人名1”,年龄,‘男’,分数1,分数2)。将生成的插入语句输出到记事本文件 SQLStr.txt中