国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

ashx文件的使用小結

2024-07-10 12:44:12
字體:
來源:轉載
供稿:網友

一提到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文件

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 沁源县| 江川县| 鸡东县| 南木林县| 姜堰市| 商河县| 陇西县| 德清县| 云阳县| 包头市| 富川| 忻城县| 兴宁市| 古田县| 临夏县| 吴堡县| 临江市| 瓦房店市| 新闻| 保靖县| 竹山县| 南充市| 荆门市| 勐海县| 女性| 沧州市| 夏邑县| 乌拉特前旗| 施甸县| 兴化市| 兴安县| 东城区| 含山县| 云南省| 琼结县| 云安县| 炉霍县| 岳阳市| 平遥县| 抚松县| 兴安县|