Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将信息传递到ashx页面并确定哪个图像将出现在asp图像控件中_C#_Asp.net_Image_Handler - Fatal编程技术网

C# 将信息传递到ashx页面并确定哪个图像将出现在asp图像控件中

C# 将信息传递到ashx页面并确定哪个图像将出现在asp图像控件中,c#,asp.net,image,handler,C#,Asp.net,Image,Handler,如何将信息传递给处理程序页面?如何指定asp图像控件中显示的图像 Handler1.ashx.cs代码: public void ProcessRequest(HttpContext context) { int id1 = something //how can I pass a information to a handler page int id2 = somthing 2 // same case

如何将信息传递给处理程序页面?如何指定asp图像控件中显示的图像

Handler1.ashx.cs代码:

public void ProcessRequest(HttpContext context)
        {
             int id1 = something //how can I pass a information to a handler page
             int id2 = somthing 2 // same case

                byte[] IMG = classP.RedImg(id1);
                context.Response.ContentType = "image/jpg";
                context.Response.BinaryWrite(IMG);

                byte[] IMG2 = classP.RedImg(id2);
                context.Response.ContentType = "image/jpg";
                context.Response.BinaryWrite(IMG2);
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
Page.aspx代码:

<asp:Image ID="Image1" runat="server" />
<asp:Image ID="Image2" runat="server" />
谢谢:

两件事:

  • 每个请求将调用一次处理程序。因此,像这样返回两个图像将不起作用
  • ProcessRequest
    中,需要
    context.Request.QueryString[“ID”]
    来获取要添加到codebehind中的ID查询字符串参数 您必须对该值调用
    int.Parse
    int.TryParse
    ,因为
    RedImg
    似乎期望一个int。最好的做法是首先从查询字符串中获取值,用
    string.IsNullOrEmpty
    检查该值,如果是,则尽早退出。然后对该值使用
    int.TryParse
    ,然后也退出返回false的选项。您可能还应该将
    context.Response.StatusCode
    设置为404或类似的设置,以使其更“合适”HTTP

    string[] data = classC.ReadClient();
    int id1 = Convert.ToInt32(data[0]); //Here is id1 value
    int id2 = Convert.ToInt32(data[1]); //Here is id2 value
    
    Image1.ImageUrl = "~/Handler1.ashx?ID=" + id1.ToString();
    Image2.ImageUrl = "~/Handler1.ashx?ID=" + id2.ToString();