温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

ashx文件的使用方法有哪些

发布时间:2021-10-08 15:17:18 来源:亿速云 阅读:137 作者:iii 栏目:开发技术

这篇文章主要讲解了“ashx文件的使用方法有哪些”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“ashx文件的使用方法有哪些”吧!

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

感谢各位的阅读,以上就是“ashx文件的使用方法有哪些”的内容了,经过本文的学习后,相信大家对ashx文件的使用方法有哪些这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI