为什么我会收到“发送 HTTP 标头后无法重定向”信息 当我调用 Response.Redirect() 时?
当我调用 Response.Redirect(someUrl) 时,我收到以下 HttpException:
发送 HTTP 标头后无法重定向。
为什么我会得到这个? 我该如何解决这个问题?
When I call Response.Redirect(someUrl)
I get the following HttpException:
Cannot redirect after HTTP headers have been sent.
Why do I get this? And how can I fix this issue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
重定向功能可能通过使用“刷新”http 标头(也可能使用 30X 代码)来工作。 一旦标头发送到客户端,服务器就无法附加该重定向命令,为时已晚。
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.
如果您在发送 HTTP 标头后收到无法重定向,请尝试以下代码。
If you get Cannot redirect after HTTP headers have been sent then try this below code.
最后一个参数如果设置为 False,可以解决问题,但不会终止当前线程的响应。
The last parameter, if it is set to False, solves the issue but does not terminate the response for the current thread.
有 2 种方法可以解决此问题:
只需在
Response.Redirect(someUrl);
之后添加一个return
语句即可(如果方法签名不是“void”,当然,您必须返回该“类型”)
如下:
Response.Redirect("Login.aspx");
return;
请注意,返回允许服务器执行重定向...如果没有它,服务器将继续执行其余代码...
//......一些代码
.....一些逻辑
......更多代码
// 将你的 Response.Redirect 移动到这里(方法的结尾):
There are 2 ways to fix this:
Just add a
return
statement after yourResponse.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...
Response.Redirect(someUrl)
the LAST executed statement in the method that is throwing the exception. Replace yourResponse.Redirect(someUrl)
with a string VARIABLE named "someUrl", and set it to the redirect location... as follows://......some code
.....some logic
......more code
// MOVE your Response.Redirect to HERE (the end of the method):
根据 Response.Redirect(string url) 的 MSDN 文档,当“发送 HTTP 标头后尝试重定向”时,它将抛出 HttpException。 由于 Response.Redirect(string url) 使用 Http“Location”响应标头 (http://en.wikipedia.org/wiki/HTTP_headers#Responses),调用它将导致标头发送到客户端。 这意味着,如果您第二次调用它,或者在以其他方式发送标头之后调用它,您将收到 HttpException。
防止多次调用 Response.Redirect() 的一种方法是在调用之前检查 Response.IsRequestBeingRedirected 属性 (bool)。
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". SinceResponse.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.一旦您将任何内容发送到客户端,HTTP 标头就已经被发送。
Response.Redirect()
调用的工作原理是在标头中发送特殊信息,使浏览器请求不同的 URL。由于标头已发送,asp.net 无法执行您想要的操作(修改标头)。
您可以通过 a) 在执行其他操作之前执行重定向,或 b) 尝试使用 Response 来解决此问题。在执行任何其他操作之前,请设置 Buffer = true,以确保在整个页面执行完毕之前不会将任何输出发送到客户端。
Once you send any content at all to the client, the HTTP headers have already been sent. A
Response.Redirect()
call works by sending special information in the headers that make the browser ask for a different URL.Since the headers were already sent, asp.net can't do what you want (modify the headers)
You can get around this by a) either doing the Redirect before you do anything else, or b) try using
Response.Buffer = true
before you do anything else, to make sure that no output is sent to the client until the whole page is done executing.仅当 HTTP 消息中的第一行是“
HTTP/1.x 3xx Redirect Reason
”时,才会发生重定向。如果您已经调用了 Response.Write() 或设置了一些标头,那么重定向就为时已晚。 您可以尝试在重定向之前调用 Response.Headers.Clear() 看看是否有帮助。
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 callingResponse.Headers.Clear()
before the Redirect to see if that helps.只需检查您是否已将缓冲选项设置为 false(默认情况下为 true)。 为了使response.redirect工作,
Just check if you have set the buffering option to false (by default its true). For response.redirect to work,
使用
return RedirectPermanent(myUrl)
为我工作Using
return RedirectPermanent(myUrl)
worked for me您还可以使用下面提到的代码
You can also use below mentioned code
对于这个问题有一个简单的答案:
在发送标头之前,您已经输出了其他内容,例如文本或与页面输出相关的任何内容。 这会影响您收到该错误的原因。
只需检查您的代码是否有可能的输出,或者您可以将标头放在方法的顶部,以便首先发送它。
There is one simple answer for this:
You have been output something else, like text, or anything related to output from your page before you send your header. This affect why you get that error.
Just check your code for posible output or you can put the header on top of your method so it will be send first.
如果您在发送标头后尝试重定向(例如,您正在从部分生成的页面执行错误重定向),则可以发送一些客户端 Javascript(location.replace 或 location.href 等)重定向到您想要的任何 URL。 当然,这取决于已经发送下来的 HTML 内容。
If you are trying to redirect after the headers have been sent (if, for instance, you are doing an error redirect from a partially-generated page), you can send some client Javascript (location.replace or location.href, etc.) to redirect to whatever URL you want. Of course, that depends on what HTML has already been sent down.
通过添加异常处理程序来处理我的问题得到解决
“发送 HTTP 标头后无法重定向”。 这个错误如下代码所示
My Issue got resolved by adding the Exception Handler to handle
"Cannot redirect after HTTP headers have been sent". this Error as shown below code
我使用以下方法解决了问题:
Response.RedirectToRoute("CultureEnabled", RouteData.Values);
而不是 Response.Redirect。
I solved the problem using:
Response.RedirectToRoute("CultureEnabled", RouteData.Values);
instead of Response.Redirect.
确保在重定向部分之前不要使用
Response
的方法,例如Response.Flush();
。Be sure that you don't use
Response
s' methods likeResponse.Flush();
before your redirecting part.错误
发送 HTTP 标头后无法重定向。
System.Web.HttpException (0x80004005):发送 HTTP 标头后无法重定向。
建议
如果我们使用 asp.net mvc 并在同一控制器上工作并重定向到不同的 Action,那么您不需要编写..
Response.Redirect("ActionName","ControllerName");< br> 最好只使用
return RedirectToAction("ActionName");
或
return View("ViewName");
Error
Cannot redirect after HTTP headers have been sent.
System.Web.HttpException (0x80004005): Cannot redirect after HTTP headers have been sent.
Suggestion
If we use asp.net mvc and working on same controller and redirect to different Action then you do not need to write..
Response.Redirect("ActionName","ControllerName");
its better to use only
return RedirectToAction("ActionName");
or
return View("ViewName");