当我调用Response.Redirect(someUrl)
时,我收到一个HttpException:“在发送HTTP标头后无法重定向”。
为什么我会这样?我该如何解决这个问题呢?
答案 0 :(得分:116)
根据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)。
// 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");
答案 1 :(得分:17)
将任何内容发送到客户端后,HTTP标头已经发送。 Response.Redirect()
调用通过在标头中发送特殊信息来使浏览器请求不同的URL。
由于标题已经发送,asp.net无法执行您想要的操作(修改标题)
你可以通过a)在你做其他事情之前做重定向,或b)在你做任何其他事情之前尝试使用Response.Buffer = true
来解决这个问题,以确保在客户端之前没有输出任何输出整页完成执行。
答案 2 :(得分:8)
只有在HTTP消息中的第一行是“HTTP/1.x 3xx Redirect Reason
”时才会发生重定向。
如果您已拨打Response.Write()
或设置了一些标题,那么重定向就太晚了。您可以尝试在重定向之前调用Response.Headers.Clear()
以查看是否有帮助。
答案 3 :(得分:3)
只需检查您是否已将缓冲选项设置为false(默认情况下为true)。对于response.redirect工作,
答案 4 :(得分:2)
使用
Module 'adress.services' is not available!
为我工作
答案 5 :(得分:1)
有一个简单的答案: 在发送标题之前,您已经输出了其他内容,例如文本或与页面输出相关的任何内容。这会影响您获得该错误的原因。
只需检查您的代码是否有可能输出,或者您可以将标题放在方法的顶部,以便首先发送。
答案 6 :(得分:1)
如果您在发送标头后尝试重定向(例如,如果您从部分生成的页面执行错误重定向),则可以发送一些客户端Javascript(location.replace或location.href,等)重定向到你想要的任何URL。当然,这取决于已发送的HTML。
答案 7 :(得分:1)
我的问题通过添加Exception Handler来解决 “在发送HTTP标头后无法重定向”。此错误如下所示代码
catch (System.Threading.ThreadAbortException)
{
// To Handle HTTP Exception "Cannot redirect after HTTP headers have been sent".
}
catch (Exception e)
{//Here you can put your context.response.redirect("page.aspx");}
答案 8 :(得分:1)
我使用以下方法解决了问题: Response.RedirectToRoute(“CultureEnabled”,RouteData.Values); 而不是Response.Redirect。
答案 9 :(得分:0)
重定向功能可能通过使用'刷新'http标头(也可能使用30X代码)来工作。一旦将标头发送到客户端,服务器就无法附加该重定向命令,为时已晚。
答案 10 :(得分:0)
如果您在发送HTTP标头后无法重定向,请尝试以下代码。
HttpContext.Current.Server.ClearError();
// Response.Headers.Clear();
HttpContext.Current.Response.Redirect("/Home/Login",false);
答案 11 :(得分:0)
请确保您不使用Response
s'重定向部分之前的Response.Flush();
等方法。
答案 12 :(得分:0)
您也可以使用下面提到的代码
Response.Write("<script type='text/javascript'>"); Response.Write("window.location = '" + redirect url + "'</script>");Response.Flush();
答案 13 :(得分:0)
错误
发送HTTP标头后无法重定向。
System.Web.HttpException(0x80004005):发送HTTP标头后无法重定向。
建议
如果我们使用asp.net mvc并在同一个控制器上工作并重定向到其他Action,则无需编写。
Response.Redirect(“ ActionName”,“ ControllerName”);
最好只使用
return RedirectToAction(“ ActionName”);
或
return View(“ ViewName”);
< / p>
答案 14 :(得分:-3)
有两种方法可以解决这个问题:
只需在Response.Redirect(someUrl);
之后添加Response.Redirect(someUrl)
语句
(如果方法签名不是&#34; void&#34;,你必须返回&#34;键入&#34;当然)
如此:
的Response.Redirect(&#34;的Login.aspx&#34);
返回;
注意返回允许服务器执行重定向...没有它,服务器想继续执行其余的代码......
Response.Redirect(someUrl)
LAST执行语句。将//......some code
替换为名为&#34; someUrl&#34;的字符串VARIABLE,并将其设置为重定向位置...如下所示: string someUrl = String.Empty
if (x=y)
{
// comment (original location of Response.Redirect("Login.aspx");)
someUrl = "Login.aspx";
}
......一些逻辑
Response.Redirect(someUrl);
return;
......更多代码
//将您的Response.Redirect移动到此处(方法结束):
<Menu guid="guidPackageCmdSet" id="Toolbar" type="Toolbar">
<CommandFlag>DefaultDocked</CommandFlag>
<Strings>
<ButtonText>My Toolbar</ButtonText>
<CommandName>My Toolbar</CommandName>
</Strings>
</Menu>
<Group guid="guidPackageCmdSet" id="ToolbarGroup" priority="0x0000">
<Parent guid="guidPackageCmdSet" id="Toolbar" />
</Group>
<GuidSymbol name="guidPackageCmdSet" value="{498fdff5-5217-4da9-88d2-edad44ba3874}">
<IDSymbol name="Toolbar" value="0x1000" />
<IDSymbol name="ToolbarGroup" value="0x1050" />
</GuidSymbol>