【发布时间】:2009-03-17 17:40:35
【问题描述】:
在一个 ASP.NET 3.5 应用程序中,我创建了一个 ashx 处理程序,如下所示:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Web.Services;
namespace TestWebConfig
{
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Handler1 : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "image/jpg";
BinaryReader br = new BinaryReader(File.Open( @"d:\Temp\images\AutumnLeaves.jpg", FileMode.Open ));
int bufferLength = 1000;
do
{
byte[] buffer = br.ReadBytes(bufferLength);
context.Response.OutputStream.Write(buffer, 0, buffer.Length);
if (buffer.Length < bufferLength)
{
break;
}
} while (true);
br.Close();
}
public bool IsReusable
{
get
{
return true;
}
}
}
}
在一个aspx页面中,如果我指定:
<img alt="alt2" src="Handler1.ashx" style="border-width:0px"/>
然后网页与图像一起加载到浏览器中。另一方面,如果我使用:
<asp:Image ID="Image2" runat="server" />
在代码隐藏中:
protected void Page_Load(object sender, EventArgs e)
{
Image2.ImageUrl = "Handler1.asxh";
}
那么 Image2 控件不会加载图片,尽管相关的 html 代码看起来很相似。仅显示替代文本。怎么了?
谢谢
【问题讨论】:
-
问:为什么上面有[WebService]和[WebServiceBinding]属性?它们与 ASHX 无关。
标签: asp.net httphandler