【问题标题】:Ashx handler cannot be used through an <asp:Image> controlAshx 处理程序不能通过 <asp:Image> 控件使用
【发布时间】: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


【解决方案1】:

对于您的 Page_Load 中的一件事,您似乎给了它错误的扩展名,但我认为这不是问题。

【讨论】:

  • 大声笑,我认为您可能是手动输入代码而不是复制粘贴。很抱歉的假设。
  • 没关系..在代码隐藏中编写字符串文字对我来说是一个持续的错误来源:(
猜你喜欢
  • 2010-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-15
  • 2013-03-16
  • 2014-07-27
  • 1970-01-01
  • 2012-10-09
相关资源
最近更新 更多