Ashx文件的使用方法

news/2025/3/14 17:22:03/

一提到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"]);
   }
}

 

 

 

 

 

 

 

转载于:https://www.cnblogs.com/hxwzwiy/archive/2012/03/22/2412264.html


http://www.ppmy.cn/news/829334.html

相关文章

ashx 文件的运用

在ASP.NET中有一种这样格式的文件ashx文件&#xff0c;作什么用的呢&#xff1f;如果你想创建一个ASP.NET文件&#xff0c;它不是aspx文件&#xff0c;它能动态的返回一个图片、XML文件或其他非HTML文件。那就使用ashx文件了。下面介绍下使用方法&#xff1a;1. 使用ASHX handl…

.ashx文件

ashx是什么文件,如何创建 .ashx 文件用于写web handler的。其实就是带HTML和C#的混合文件。当然你完全可以用.aspx 的文件后缀。使用.ashx 可以让你专注于编程而不用管相关的WEB技术。.ashx必须包含IsReusable.建立方法如下&#xff1a; 首先打开一个Web项目&#xff0c;然后在…

ashx文件

ashx是什么文件?   .ashx 文件用于写web handler的。.ashx文件与.aspx文件类似&#xff0c;可以通过它来调用HttpHandler类&#xff0c;它免去了普通.aspx页面的控件解析以及页面处理的过程。其实就是带HTML和C#的混合文件。 .ashx文件适合产生供浏览器处理的、不需要回发处…

ashx文件的使用

最近在研究DTcms,ashx用于生成登陆的code,所以研究了一下。 ashx是什么文件 .ashx 文件用于写web handler的。.ashx文件与.aspx文件类似&#xff0c;可以通过它来调用HttpHandler类&#xff0c;它免去了普通.aspx页面的控件解析以及页面处理的过程。其实就是带HTML和C#的混合文…

ashx 文件怎么用

本文导读&#xff1a;ashx是什么文件,如何创建 .ashx 文件用于写web handler的。其实就是带HTML和C#的混合文件。.ashx文件类似于.aspx文件&#xff0c;可以通过它来调用HttpHandler类&#xff0c;从而免去了普通.aspx页面的控件解析以及页面处理的过程。 一、ashx文件的添加 打…

图像处理之图像灰度化

图像灰度化 将彩色图像转化成为灰度图像的过程成为图像的灰度化处理。彩色图像中的每个像素的颜色有R、G、B三个分量决定&#xff0c;而每个分量有255中值可取&#xff0c;这样一个像素点可以有1600多万 (255255255)的颜色的变化范用。而灰度图像是R、G、B三个分量相同的一种特…

【C++ 重要知识点总结】Boost C++ 库 Asio

本章介绍了 Boost C 库 Asio&#xff0c;它是异步输入输出的核心。 名字本身就说明了一切&#xff1a;Asio 意即异步输入/输出。 该库可以让 C 异步地处理数据&#xff0c;且平台独立。 异步数据处理就是指&#xff0c;任务触发后不需要等待它们完成。 相反&#xff0c;Boost.A…

QQ同步登入出现 回调地址非法,请使用已注册的回调地址(21006)!错误解决办法

错误信息&#xff1a;回调地址非法&#xff0c;请使用已注册的回调地址(21006)&#xff01; 解决办法是在&#xff1a; 只需要在connect.qq.com后台将回调地址参数修改为http://www.xxx.com(注你的域名)就可以解决&#xff0c;修改后在1-2小时内将会生效。 <script>docum…