当前位置: 首页 > news >正文

ashx文件的使用[转]

原文:http://www.cnblogs.com/jerry0925/articles/901936.html

 

Ashx文件,我们可用用来作图片加载(在之前我们一般使用ASPX或者Webservice去做),一般做法如下:

Handler.ashx:

 

 1 <% @ WebHandler Language = " C# "  Class = " Handler "   %>
 2 using  System;
 3 using  System.IO;
 4 using  System.Web;
 5 ExpandedBlockStart.gifContractedBlock.gif public   class  Handler : IHttpHandler  {
 6
 7ExpandedSubBlockStart.gifContractedSubBlock.gifpublic bool IsReusable {
 8ExpandedSubBlockStart.gifContractedSubBlock.gif  get {
 9   return true;
10  }

11}

12ExpandedSubBlockStart.gifContractedSubBlock.gifpublic void ProcessRequest (HttpContext context) {
13  context.Response.ContentType = "image/jpeg";
14  context.Response.Cache.SetCacheability(HttpCacheability.Public);
15  context.Response.BufferOutput = false;
16  PhotoSize size;
17ExpandedSubBlockStart.gifContractedSubBlock.gif  switch (context.Request.QueryString["Size"]) {
18   case "S":
19    size = PhotoSize.Small;
20    break;
21   case "M":
22    size = PhotoSize.Medium;
23    break;
24   case "L":
25    size = PhotoSize.Large;
26    break;
27   default:
28    size = PhotoSize.Original;
29    break;
30  }
 
31  Int32 id = -1;
32  Stream stream = null;
33ExpandedSubBlockStart.gifContractedSubBlock.gif  if (context.Request.QueryString["PhotoID"!= null && context.Request.QueryString["PhotoID"!= ""{
34   id = Convert.ToInt32(context.Request.QueryString["PhotoID"]);
35   stream = PhotoManager.GetPhoto(id, size);
36ExpandedSubBlockStart.gifContractedSubBlock.gif  }
 else {
37   id = Convert.ToInt32(context.Request.QueryString["AlbumID"]);
38   stream = PhotoManager.GetFirstPhoto(id, size);
39  }

40  if (stream == null) stream = PhotoManager.GetPhoto(size);
41  const int buffersize = 1024 * 16;
42  byte[] buffer = new byte[buffersize];
43  int count = stream.Read(buffer, 0, buffersize);
44ExpandedSubBlockStart.gifContractedSubBlock.gif  while (count > 0{
45   context.Response.OutputStream.Write(buffer, 0, count);
46   count = stream.Read(buffer, 0, buffersize);
47  }

48}

49}

50
51

*.aspx:
<img src="myHttpHander.ashx?id=123" width="20" height="20" />


我们变通以下,发现其实除了可以输出图片以外,还可以输出文字:
Handler.ashx:

 1 <% @ WebHandler Language = " C# "  Class = " Handler "   %>
 2 using  System;
 3 using  System.Web;
 4 ExpandedBlockStart.gifContractedBlock.gif public   class  Handler : IHttpHandler  {
 5    
 6ExpandedSubBlockStart.gifContractedSubBlock.gif    public void ProcessRequest (HttpContext context) {
 7        context.Response.ContentType = "text/plain";
 8        context.Response.Write("alert('hi')");
 9    }

10
11ExpandedSubBlockStart.gifContractedSubBlock.gif    public bool IsReusable {
12ExpandedSubBlockStart.gifContractedSubBlock.gif        get {
13            return false;
14        }

15    }

16}

17

*.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:

 1 <% @ WebHandler Language = " C# "  Class = " TestHandler "   %>
 2 using  System;
 3 using  System.Web;
 4 ExpandedBlockStart.gifContractedBlock.gif public   class  TestHandler : IHttpHandler  {
 5    
 6ExpandedSubBlockStart.gifContractedSubBlock.gif    public void ProcessRequest (HttpContext context) {
 7        context.Response.ContentType = "text/plain";
 8        context.Response.Write("document.write(\"Hello World\");");
 9    }

10
11
12
13ExpandedSubBlockStart.gifContractedSubBlock.gif    public bool IsReusable {
14ExpandedSubBlockStart.gifContractedSubBlock.gif        get {
15            return false;
16        }

17    }

18}

19
20

*.aspx:
<script type="text/javascript" src="TestHandler.ashx" />

相关文章:

  • Python备份目录及目录下的全部内容
  • MS CRM 2011 RetrieveMultiple with JScript JQuery Silverlight LINQ FetchXML and QueryExpression
  • 初识SOA
  • 需求:需求获取技术之原型
  • Windows Azure HandBook (4) 分析Windows Azure如何处理Session
  • 树莓派练习程序(声音检测)
  • .NET 4.0中的泛型协变和反变
  • flex 属性绑定
  • 常用方法
  • asp.net页面间传值的9种方式
  • 用操作符函数格式化(c++)
  • HDU4626+博弈
  • Android游戏快速入门(一):基础储备
  • 应用开发框架之——插件、包
  • SQL SERVER中强制类型转换cast和convert的区别
  • 《Java8实战》-第四章读书笔记(引入流Stream)
  • CEF与代理
  • crontab执行失败的多种原因
  • Docker下部署自己的LNMP工作环境
  • JavaScript DOM 10 - 滚动
  • Java多线程(4):使用线程池执行定时任务
  • KMP算法及优化
  • node入门
  • Python实现BT种子转化为磁力链接【实战】
  • 技术:超级实用的电脑小技巧
  • 前端技术周刊 2019-02-11 Serverless
  • 前端面试之CSS3新特性
  • 使用docker-compose进行多节点部署
  • 学习笔记:对象,原型和继承(1)
  • 正则学习笔记
  • No resource identifier found for attribute,RxJava之zip操作符
  • 从如何停掉 Promise 链说起
  • 基于django的视频点播网站开发-step3-注册登录功能 ...
  • !!java web学习笔记(一到五)
  • #git 撤消对文件的更改
  • #我与Java虚拟机的故事#连载16:打开Java世界大门的钥匙
  • (JSP)EL——优化登录界面,获取对象,获取数据
  • (pojstep1.1.2)2654(直叙式模拟)
  • (编程语言界的丐帮 C#).NET MD5 HASH 哈希 加密 与JAVA 互通
  • (附源码)springboot家庭装修管理系统 毕业设计 613205
  • (南京观海微电子)——COF介绍
  • (牛客腾讯思维编程题)编码编码分组打印下标题目分析
  • **PHP分步表单提交思路(分页表单提交)
  • *1 计算机基础和操作系统基础及几大协议
  • .NET Core 项目指定SDK版本
  • .NET Framework Client Profile - a Subset of the .NET Framework Redistribution
  • .net mvc actionresult 返回字符串_.NET架构师知识普及
  • @require_PUTNameError: name ‘require_PUT‘ is not defined 解决方法
  • @zabbix数据库历史与趋势数据占用优化(mysql存储查询)
  • [《百万宝贝》观后]To be or not to be?
  • [AAuto]给百宝箱增加娱乐功能
  • [BetterExplained]书写是为了更好的思考(转载)
  • [BUUCTF 2018]Online Tool(特详解)
  • [BZOJ 1032][JSOI2007]祖码Zuma(区间Dp)
  • [C# WPF] DataGrid选中行或选中单元格的背景和字体颜色修改