Why do I get “Cannot redirect after HTTP headers h

2019-01-03 02:21发布

When I call Response.Redirect(someUrl) I get an HttpException: "Cannot redirect after HTTP headers have been sent".

Why do I get this? And how can I fix this issue?

15条回答
Summer. ? 凉城
2楼-- · 2019-01-03 03:00

According to the MSDN documentation for Response.Redirect(string url), it will throw an HttpException when "a redirection is attempted after the HTTP headers have been sent". Since Response.Redirect(string url) uses the Http "Location" response header (http://en.wikipedia.org/wiki/HTTP_headers#Responses), calling it will cause the headers to be sent to the client. This means that if you call it a second time, or if you call it after you've caused the headers to be sent in some other way, you'll get the HttpException.

One way to guard against calling Response.Redirect() multiple times is to check the Response.IsRequestBeingRedirected property (bool) before calling it.

// Causes headers to be sent to the client (Http "Location" response header)
Response.Redirect("http://www.stackoverflow.com");
if (!Response.IsRequestBeingRedirected)
    // Will not be called
    Response.Redirect("http://www.google.com");
查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-01-03 03:02

There are 2 ways to fix this:

  1. Just add a return statement after your Response.Redirect(someUrl); ( if the method signature is not "void", you will have to return that "type", of course ) as so:

    Response.Redirect("Login.aspx");

    return;

Note the return allows the server to perform the redirect...without it, the server wants to continue executing the rest of your code...

  1. Make your Response.Redirect(someUrl) the LAST executed statement in the method that is throwing the exception. Replace your Response.Redirect(someUrl) with a string VARIABLE named "someUrl", and set it to the redirect location... as follows:

//......some code

string someUrl = String.Empty

.....some logic

if (x=y)
{
    // comment (original location of Response.Redirect("Login.aspx");)
    someUrl = "Login.aspx";
}

......more code

// MOVE your Response.Redirect to HERE (the end of the method):

Response.Redirect(someUrl);
return; 
查看更多
男人必须洒脱
4楼-- · 2019-01-03 03:04

A Redirect can only happen if the first line in an HTTP message is "HTTP/1.x 3xx Redirect Reason".

If you already called Response.Write() or set some headers, it'll be too late for a redirect. You can try calling Response.Headers.Clear() before the Redirect to see if that helps.

查看更多
放荡不羁爱自由
5楼-- · 2019-01-03 03:05

Be sure that you don't use Responses' methods like Response.Flush(); before your redirecting part.

查看更多
兄弟一词,经得起流年.
6楼-- · 2019-01-03 03:07

Using return RedirectPermanent(myUrl) worked for me

查看更多
我命由我不由天
7楼-- · 2019-01-03 03:09

The redirect function probably works by using the 'refresh' http header (and maybe using a 30X code as well). Once the headers have been sent to the client, there is not way for the server to append that redirect command, its too late.

查看更多
登录 后发表回答