溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

ashx文件的使用方法有哪些

發(fā)布時(shí)間:2021-10-08 15:17:18 來源:億速云 閱讀:156 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要講解了“ashx文件的使用方法有哪些”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“ashx文件的使用方法有哪些”吧!

一提到Ashx文件,我們就會(huì)想到http handler以及圖片加載(在之前我們一般使用ASPX或者Webservice去做),一般做法如下:

Handler.ashx:

復(fù)制代碼 代碼如下:

<%@ 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:

復(fù)制代碼 代碼如下:


<%@ 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:

復(fù)制代碼 代碼如下:


<%@ 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í),你必須實(shí)現(xiàn)IReadOnlySessionState接口.

代碼:

復(fù)制代碼 代碼如下:

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文件的使用方法有哪些”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對(duì)ashx文件的使用方法有哪些這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI