开发者

Response.Redirect Fails When Called By "Inner" ASPX

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.asp开发者_运维百科x", true) but to no avail. Any ideas how I could fix this?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜