将数据从 ashx 传输到 aspx.使 asp 图像控件标识要显示的图像 - asp.net c#



page.aspx.cs code:

HttpContext.Current.Session["id1"] = id1.ToString();
Server.Transfer("Handler.ashx"); //Getting error
Image1.ImageUrl = "~/Handler.ashx?ID=" + id1.ToString();
HttpContext.Current.Session["id2"] = id2.ToString();
Server.Transfer("Handler.ashx"); //Getting error
Image2.ImageUrl = "~/Handler.ashx?ID=" + id2.ToString();

Handler.ashx 代码:

public void ProcessRequest(HttpContext context)
{
    int id = Convert.ToInt32(context.Session["id1"]);
    byte[] IMG = class.ReadImg(id);
    context.Response.ContentType = "image/jpg";
    context.Response.BinaryWrite(IMG);
}
public void ProcessRequest2(HttpContext context2)
{
    int id = Convert.ToInt32(context2.Session["id2"]);
    byte[] IMG2 = class.ReadImg(id);
    context2.Response.ContentType = "image/jpg";
    context2.Response.BinaryWrite(IMG2);
}

页面.aspx代码:

<asp:Image ID="Image1" runat="server" />//How to define thats gonna show image of id1
<asp:Image ID="Image2" runat="server" />//How to define thats gonna show image of id2

问题:

  1. 我无法将数据从页面.aspx传输到 Handler.ashx
  2. 我不知道如何制作Image元素来识别它应该根据 id 显示哪个图像(也许是我不明白的一些request.queryString魔法)

帮助!!谢谢 :]

Server.Transfer() 按照它所说的去做 - 在服务器端传输请求,而不返回 HTTP 301 重定向到客户端。

只需读取 ashx 文件中的查询字符串即可解决您的问题:

string id = context.Request.Querystring["ID"];
context.Response.ContentType="image/jpeg";
......

然后在 aspx 页中:

Image1.ImageUrl = "/Handler.ashx?ID="+id.ToString();