博客后台切换至i.cnblogs.com之后,在日志中发现大量的“无法在发送HTTP标头之后进行重定向”(Cannot redirect after HTTP headers have been sent)的错误信息。

检查代码发现问题是由下面的代码触发的:

IHttpHandler IHttpHandlerFactory.GetHandler(HttpContext context, string requestType, string url, string pathTranslated)
{
context.Response.Redirect("http://i.cnblogs.com/" +
context.Request.RawUrl.Substring(context.Request.RawUrl.LastIndexOf("/") + )); //后续也有context.Response.Redirect代码
//...
return PageParser.GetCompiledPageInstance(newurl, path, context);
}

“无法在发送HTTP标头之后进行重定向”问题来源于Response.Redirect之后,又进行了Response.Redirect。

解决方法很简单:在Response.Redirect之后立即返回。

IHttpHandler IHttpHandlerFactory.GetHandler(HttpContext context, string requestType, string url, string pathTranslated)
{
context.Response.Redirect("http://i.cnblogs.com/" +
context.Request.RawUrl.Substring(context.Request.RawUrl.LastIndexOf("/") + ));
return null;
//...
}

为什么之前没有加return null呢?因为以前一直以为Response.Redirect会结束当前请求,不会执行Response.Redirect之后的代码。

现在残酷的现实说明了不完全是这样的,那问题背后的真相是什么?让我们来一探究竟。

由于微软公开了.NET Framework的源代码,现在无需再看Reflactor出来的代码,可以直接下载源代码用Visual Studio进行查看。

.NET Framework源代码下载链接:http://referencesource.microsoft.com/download.html (相关新闻:微软开放了.NET 4.5.1的源代码

用Visual Studio打开DotNetReferenceSource\Source\ndp.sln,搜索HttpResponse.cs,找到Response.Redirect的实现代码:

public void Redirect(String url)
{
Redirect(url, true, false);
}

实际调用的是internal void Redirect(String url, bool endResponse, bool permanent) ,传给endResponse的值的确是true啊,为什么后面的代码还会执行?

进一步查看internal void Redirect()的实现代码(省略了无关代码):

internal void Redirect(String url, bool endResponse, bool permanent)
{
//... Page page = _context.Handler as Page;
if ((page != null) && page.IsCallback) {
//抛异常
} // ... url处理 Clear(); //Clears all headers and content output from the buffer stream. //...
this.StatusCode = permanent ? : ; //进行重定向操作
//...
_isRequestBeingRedirected = true; var redirectingHandler = Redirecting;
if (redirectingHandler != null) {
redirectingHandler(this, EventArgs.Empty);
} if (endResponse)
End(); //结束当前请求
}

从上面的代码可以看出,我们要找的真相在End()方法中,继续看HttpResponse.End()的实现代码:

public void End() {
if (_context.IsInCancellablePeriod) {
AbortCurrentThread();
}
else {
// when cannot abort execution, flush and supress further output
_endRequiresObservation = true; if (!_flushing) { // ignore Reponse.End while flushing (in OnPreSendHeaders)
Flush();
_ended = true; if (_context.ApplicationInstance != null) {
_context.ApplicationInstance.CompleteRequest();
}
}
}
}

注意啦!真相浮现了!

以前一直以为的Response.Redirect会结束当前请求,就是上面的AbortCurrentThread()情况,如果将Response.Redirect放在try...catch中就会捕捉到ThreadAbortException异常。

通常情况下,我们在WebForms的Page或MVC的Controller中进行Redirect,_context.IsInCancellablePeriod的值为true,执行的是AbortCurrentThread(),所以不会遇到这个问题。

而我们现在的场景恰恰是因为_context.IsInCancellablePeriod的值为false,为什么会是false呢?

进一步看一下_context.IsInCancellablePeriod的实现:

private int _timeoutState; // 0=non-cancelable, 1=cancelable, -1=canceled

internal bool IsInCancellablePeriod {
get { return (Volatile.Read(ref _timeoutState) == ); }
}

根据上面的代码,触发这个问题的条件是_timeoutState的值要么是0,要么是-1,根据我们的实际情况,应该是0=non-cancelable。

再来看看我们的实际应用场景,我们是在实现IHttpHandlerFactory接口的GetHandler方法中进行Response.Redirect操作的,也就是说在这个阶段_timeoutState的值还没被设置(默认值就是0)。为了验证这个想法,继续看一下_timeoutState在哪个阶段设值的。

Shift+F12找到所有引用_timeoutState的地方,在HttpConext中发现了设置_timeoutState的方法BeginCancellablePeriod,实现代码如下:

internal void BeginCancellablePeriod() {
// It could be caused by an exception in OnThreadStart
if (Volatile.Read(ref _timeoutStartTimeUtcTicks) == -) {
SetStartTime();
} Volatile.Write(ref _timeoutState, );
}

然后再Shift+F12找到了在HttpApplication.ExecuteStep()中调用了BeginCancellablePeriod():

internal Exception ExecuteStep(IExecutionStep step, ref bool completedSynchronously)
{
//..
if (step.IsCancellable) {
_context.BeginCancellablePeriod(); // request can be cancelled from this point
}
//..
}

从上面的代码可以看出,当step.IsCancellable为true时,会调用BeginCancellablePeriod(),就不会出现这个问题。

而我们用到的IHttpHandlerFactory.GetHandler()所在的IExecutionStep的实现可能将IsCancellable设置为了false。

那IHttpHandlerFactory.GetHandler()是在哪个IExecutionStep的实现中调用的呢?

在园子里木宛城主的一篇写得非常棒的博文(ASP.NET那点不为人知的事)中找到了答案——MapHandlerExecutionStep:

当执行到MapHandlerExecutionStep时会执行如下代码获取最终执行请求:context.Handler = this._application.MapHttpHandler()。HttpApplication对象的MapHttpHandler方法将根据配置文件结合请求类型和URL以调用相应的IHttpHandlerFactory来获取HttpHandler对象。

我们再回到.NET Framework的源代码中看一看MapHandlerExecutionStep的实现:

// execution step -- map HTTP handler (used to be a separate module)
internal class MapHandlerExecutionStep : IExecutionStep {
private HttpApplication _application; internal MapHandlerExecutionStep(HttpApplication app) {
_application = app;
} void IExecutionStep.Execute() {
//...
} bool IExecutionStep.CompletedSynchronously {
get { return true;}
} bool IExecutionStep.IsCancellable {
get { return false
; }
}

}

看到有没有?IExecutionStep.IsCancellable返回的值是false。

到此,水落石出,真相大白!

请看大屏幕——

由于MapHandlerExecutionStep(调用IHttpHandlerFactory.GetHandler()的地方)返回的IsCancellable的值是false,于是在HttpApplication.ExecuteStep()执行时没有调用_context.BeginCancellablePeriod()——也就是没有把_timeoutState设置为1,_context.IsInCancellablePeriod的值就是false。从而造成在Response.Redirect中进行Response.End()时没有执行AbortCurrentThread()(通常情况下都会执行这个)。于是代码继续执行,后面又来一次Response.Redirect,最终引发了——“无法在发送HTTP标头之后进行重定向”(Cannot redirect after HTTP headers have been sent)。

Response.Redirect引起的“无法在发送HTTP标头之后进行重定向”的更多相关文章

  1. C# 无法在发送 HTTP 标头之后进行重定向

    在调试中发现错误如下: Response.Redirect引起的“无法在发送HTTP标头之后进行重定向” 跳转失败 解决方案如下: 使用js方法来跳转地址 const string url=" ...

  2. Page.Response.Buffer与Response.Redirect一起用报错“无法在发送 HTTP 标头之后进行重定向”

    Page.Response.Buffer与Response.Redirect一起用报错“无法在发送 HTTP 标头之后进行重定向” 原因还未知..

  3. 解决:无法在发送 HTTP 标头之后进行重定向。 跟踪信息: 在 System.Web.HttpResponse.Redirect(String url, Boolean endResponse, Boolean permanent) 在 System.Web.Mvc.Async.AsyncControllerActionInvoker.<>……

    问题:在MVC的过滤器中验证用户状态时报如下错误:   无法在发送 HTTP 标头之后进行重定向. 跟踪信息:   在 System.Web.HttpResponse.Redirect(String  ...

  4. 处理 ASP.NET 中的异常:无法在发送 HTTP 标头之后进行重定向。

    因为在 Global.asax 中的 Application_Error 事件中添加了统一的错误处理,其中会有 Redirect 重定向到错误页面. 但是有可能有些情况下已经进行过其它重定向操作,所以 ...

  5. 出现“无法在发送 HTTP 标头之后进行重定向”问题

    如题,在Response.Redirect之后会偶尔出现“无法在发送HTTP标头之后进行重定向”问题. 是因为,已经在出现错误的代码之前进行过一次重定向了.仔细检查代码即可. 解决方法:按照逻辑移除多 ...

  6. Response.Redirect:无法在发送 HTTP 标头之后进行重定向

    URL:http://blog.163.com/asp_neter/blog/static/17510918820107258107558/ 错误出现语句:“Response.Redirect(&qu ...

  7. 无法在发送 HTTP 标头之后进行重定向

    public ActionResult Index2() { Response.Buffer = true; Response.Clear(); return Redirect("/Wech ...

  8. MVC开发中的常见错误-06-"无法在发送 HTTP 标头之后进行重定向。"

    通过监视可以看到: 原来是跳转到登录页面后,登录页面中又发送了一个GeMneuItems的请求,用于加载页面图片

  9. response.redirect和server.Transfer的差别详解

    Response.Redirect和Server.Transfer都能实现页面的跳转,但两者又有很大区别. 一 地址栏里显示的地址不同 Response.Redirect会显示跳转的网页的地址,而Se ...

随机推荐

  1. 定时调度框架Quartz随笔

    最近项目中的定时批处理用到了quartz定时任务,在此记录下quartz的配置吧,一个小demo仅供参考,也方便自己今后复习! 下面直接来步骤吧! 一.首先,要搭起能让quartz正常运行的环境,至少 ...

  2. Linux下Tomcat的安装配置

    一.下载安装对应的jdk,并配置Java环境. 官网下载地址: http://www.oracle.com/technetwork/java/javase/downloads/jdk-6u26-dow ...

  3. JS日期的获取与加减

    1)获取当前日期: var today = new Date(); 2)设定某个日期: var d = new Date("2015/1/08".replace(/-/g,&quo ...

  4. C++嵌入Python,以及两者混用

    以前项目中是C++嵌入Python,开发起来很便利,逻辑业务可以放到python中进行开发,容易修改,以及功能扩展.不过自己没有详细的研究过C++嵌入python的细节,这次详细的研究一下.首先我们简 ...

  5. Day19_IO第一天

    1.异常 1.概念      程序出现不正常的情况 2.异常体系(掌握)      Throwable           |-Error                               ...

  6. [翻译]lpeg入门教程

    原文地址:http://lua-users.org/wiki/LpegTutorial 简单匹配 LPeg是一个用于文本匹配的有力表达方式,比Lua原生的字符串匹配和标准正则表达式更优异.但是,就像其 ...

  7. ibatis批量操作补充

    ibatis批量操作  这文章的补充吧. review代码的时候发现一个页面应用排序设置功能,原先代码中,进行了循环update实现.虽然应用较少,不过无谓浪费数据库链接实在不是一个好的想法. 所以, ...

  8. AJAX部分---php-jquery-ajax;

    AJAX的应用场景 1.异步搜索过滤内容数据 2.表单异步验证 3.异步加载页面“更多”数据 4.异步处理登录 5.异步处理用户名是否注册 AJAX的主要特点 1.在不刷新页面的情况下,与服务器进行异 ...

  9. squid安装、配置、控制

    Squid 目录 [隐藏]  1 前言 2 安装 2.1 从源中安装 2.2 源码编译安装 3 基本配置 4 高级控制 5 认证 6 总结 6.1 服务器配置 6.2 用户分类 6.3 行为分类 6. ...

  10. TestDisk 恢复rm -rf 的文件

    Linux操作系统下使用TestDisk恢复已删除的文件或目录 原创作者:szyzln/2015.10.16   转载需注明原始出处! 说明: testdisk和photorec是著名的恢复数据,而绝 ...