Asp.net 使用httphandler检索图像url时出现问题

Asp.net 使用httphandler检索图像url时出现问题,asp.net,Asp.net,我正在尝试编写一个httphandler来检索图像url,然后在图像控件上显示它。我正在使用这段代码,但它不起作用 OleDbConnection mDB = new OleDbConnection( ConfigurationManager.ConnectionStrings["AccessConnection"].ConnectionString); mDB.Open(); OleDbCommand cmd = new OleDbCommand("select

我正在尝试编写一个httphandler来检索图像url,然后在图像控件上显示它。我正在使用这段代码,但它不起作用

OleDbConnection mDB = new OleDbConnection(
     ConfigurationManager.ConnectionStrings["AccessConnection"].ConnectionString);
     mDB.Open();
     OleDbCommand cmd = new OleDbCommand("select pProductId from Products where pProductId=" + context.Request.QueryString["ImageID"], mDB);
     OleDbDataReader rdr = cmd.ExecuteReader();
     rdr.Read();
     context.Response.BinaryWrite((byte[])rdr[0]);
     mDB.Close();
     context.Response.End(); */
很抱歉,我在前面的SELECT语句中造成了混淆。pProductId不包含图像的URL,而pProductImage是包含URL的字段。我使用Id来识别相应显示的图像

这是我的预期输出:

<img src="ImgHandler.ashx?pProductId=2" alt="" />

我无法放置图像这是指向我的错误消息的链接:

这是一个两步回答。 在页面中,您可以执行以下操作:

        using (OleDbConnection mDB = new OleDbConnection(ConfigurationManager.ConnectionStrings["AccessConnection"].ConnectionString))
        {
            mDB.Open();
            using (OleDbCommand cmd = new OleDbCommand("select pProductId from Products where pProductId=" + context.Request.QueryString["ImageID"], mDB))
            {
                using (OleDbDataReader rdr = cmd.ExecuteReader())
                {
                    rdr.Read();
                    context.Response.Write("<img src=\"ImgHandler.ashx?pProductId=");
                    context.Response.Write((int)rdr[0]);  // I suppose pProductId is an int, adapt accordingly
                    context.Response.Write("\" />");
                }
            }
        }
public class MyHandler : IHttpHandler
{
    public bool IsReusable
    {
        get { return true; }
    }

    public void ProcessRequest(HttpContext context)
    {
        using (OleDbConnection mDB = new OleDbConnection(ConfigurationManager.ConnectionStrings["AccessConnection"].ConnectionString))
        {
            mDB.Open();
            // select the file data (I put pData here as a placeholder, and assume it will be a byte[])
            using (OleDbCommand cmd = new OleDbCommand("select pData from Products where pProductId=" + context.Request.QueryString["ImageID"], mDB))
            {
                using (OleDbDataReader rdr = cmd.ExecuteReader())
                {
                    rdr.Read();
                    context.Response.ContentType = "image/png";// put the content type here
                    context.Response.BinaryWrite((byte[])rdr[0]);
                }
            }
        }
    }
}

请注意使用模式,它确保使用后正确清理资源。此外,理想情况下,您可以使用适当的缓存HTTP头在客户端上缓存数据,如进行了修改,因为这是另一种情况…

您是否在SELECT语句中包含实际图像?鉴于上面的输入错误,它可以是任意数量的内容。正在使用的真正代码是什么?你正在经历什么行为?是否存在异常?pProductId是否为URL?这似乎很奇怪。另外,如果你想显示一个URL,你可以只写一个IMG标记,后面跟着一个SRC属性,而不是一个字节数组。对不起,伙计们,我在asp.net和c语言方面比较弱。至于@SimonMourier,我想你是对的,我希望实现的是从我的数据库中检索url,并使用id作为标识符显示图像。通常,发布预期输出和实际输出都很好。上面的代码我应该放在哪里,它在aspx文件下?因为当我把它放在那里时,它不会工作。处理程序代码应该放在一个新的.CS文件中,类MyHandler应该在声明HTTP处理程序的web.config中声明。当我将其放在my.ashx[语法错误查询表达式'pProductId='中缺少运算符]下时出现错误消息。这是数据库系统语法的问题。这与ASP.NET/Handlers等无关。对不起,simon,我不太明白您编写的代码应该放在新的.CS文件而不是.ashx文件中?