佳木斯湛栽影视文化发展公司

主頁 > 知識(shí)庫 > ashx文件的使用小結(jié)

ashx文件的使用小結(jié)

熱門標(biāo)簽:電商新玩法 電銷業(yè)務(wù) 人工智能 百度AI接口 科大訊飛語音識(shí)別系統(tǒng) 客戶服務(wù) 國美全國運(yùn)營中心 網(wǎng)站排名優(yōu)化

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


其實(shí),學(xué)習(xí)的思路不應(yīng)該這樣,以上除了圖片外,我們都用偏了,為什么用偏了呢,因?yàn)檐浖院?jiǎn)單、實(shí)用為主,我們只是把以上純粹看成可一項(xiàng)技術(shù)而沒有把它放到軟件的地位去考慮:)
具體的用途,大家可以參考Rewirte.dll (這個(gè)dll,可以使服務(wù)器支持偽靜態(tài)的)

您可能感興趣的文章:
  • ashx中使用session的方法(獲取session值)
  • ASP.NET ASHX中獲得Session的方法
  • Asp.net在ashx文件中處理Session問題解決方法
  • 在ashx文件中使用session的解決思路
  • ashx介紹以及ashx文件與aspx文件之間的區(qū)別
  • aspx與ascx,ashx的用法總結(jié)
  • 后綴為 ashx 與 axd 的文件區(qū)別淺析
  • 基于.NET中:自動(dòng)將請(qǐng)求參數(shù)綁定到ASPX、ASHX和MVC的方法(菜鳥必看)
  • *.ashx文件不能訪問Session值的解決方法

標(biāo)簽:咸寧 POS機(jī) 南平 廈門 棗莊 益陽 攀枝花 拉薩

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《ashx文件的使用小結(jié)》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    刚察县| 大姚县| 丰城市| 和田县| 贡山| 布尔津县| 马尔康县| 衡南县| 上虞市| 锦州市| 玉门市| 平远县| 渑池县| 潼关县| 邵武市| 弥渡县| 东港市| 汾阳市| 汨罗市| 临湘市| 临潭县| 襄汾县| 大安市| 南阳市| 许昌市| 文昌市| 河源市| 新乡县| 木兰县| 克东县| 界首市| 乌兰察布市| 都昌县| 芮城县| 绥德县| 惠安县| 清苑县| 分宜县| 唐海县| 通榆县| 仁怀市|