原文: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
public
class
Handler : IHttpHandler
{
6
7
public bool IsReusable
{
8
get
{
9
return true;
10
}
11
}
12
public 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;
17
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;
33
if (context.Request.QueryString["PhotoID"] != null && context.Request.QueryString["PhotoID"] != "")
{
34
id = Convert.ToInt32(context.Request.QueryString["PhotoID"]);
35
stream = PhotoManager.GetPhoto(id, size);
36
} 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);
44
while (count > 0)
{
45
context.Response.OutputStream.Write(buffer, 0, count);
46
count = stream.Read(buffer, 0, buffersize);
47
}
48
}
49
}
50
51

2

3

4

5



6

7



8



9

10

11

12



13

14

15

16

17



18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33



34

35

36



37

38

39

40

41

42

43

44



45

46

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
public
class
Handler : IHttpHandler
{
5
6
public void ProcessRequest (HttpContext context)
{
7
context.Response.ContentType = "text/plain";
8
context.Response.Write("alert('hi')");
9
}
10
11
public bool IsReusable
{
12
get
{
13
return false;
14
}
15
}
16
}
17

2

3

4



5

6



7

8

9

10

11



12



13

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
public
class
TestHandler : IHttpHandler
{
5
6
public void ProcessRequest (HttpContext context)
{
7
context.Response.ContentType = "text/plain";
8
context.Response.Write("document.write(\"Hello World\");");
9
}
10
11
12
13
public bool IsReusable
{
14
get
{
15
return false;
16
}
17
}
18
}
19
20

2

3

4



5

6



7

8

9

10

11

12

13



14



15

16

17

18

19

20

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