views:

151

answers:

1

I have an ASPX (PictureGetter.aspx) That loads images and writes them to the response in a manner such as this:

private void WritePicture()
{
    byte[] bytes = GetBytes(picPath);
    Response.ContentType = "image/jpeg";
    Response.Clear();
    Response.BinaryWrite(bytes);
    Response.End();
}

This can then be used on pages like so:

<img src="/path/to/PictureGetter.aspx?some_param=some_value" />

However, for certain scenarios, I won't be able to get an image, so I'd like to redirect the user to an entirely different page:

if (some_condition)
{
    Response.Redirect("/another/path/page.aspx");
}
else
{
    WritePicture();
}

However, the redirection never occurs. I've tried Response.Redirect("/another/path/page.aspx", false) and Response.Redirect("/another/path/page.aspx", true) but to no avail. Any ideas how I could fix this?

+2  A: 

The request you're sending the redirect on is not for the page, but rather just for the image. You will not be able to send a redirect from the server unless the the request coming in is for the actual page.

To be more specific: If you need to redirect based on some condition you should do it when the page the image is being hosted on is requested. If you really need to do it the way you've laid out (won't know if you need to redirect until an image load is attempted), you're going to have to do it using some messy client side javascript.

David Hay
response.write("<script>");response.write("document.location.href('/another/path/page.aspx')");response.write("</script>");
adamcodes