c# - Response.End () 不会中止当前线程

标签 c# asp.net

任何人都知道为什么 ASP.NET 可能不会使用 Response.End() 中止当前线程吗?

更新:原因是有代码,尽管写得不好,在 Response.End() 之后被执行。我从未见过 Response.End () 没有阻止当前线程执行的情况。


protected void Page_Load(object sender, EventArgs e)
{
        Response.Clear();
        Response.Redirect("somewhere", true);
        Response.End();

        //Some other code get's executed here
}

最佳答案

正如您所指出的,方法 Response.End 定义为:

public void End()
{
  if (this._context.IsInCancellablePeriod) {
    InternalSecurityPermissions.ControlThread.Assert();
    Thread.CurrentThread.Abort(new HttpApplication.CancelModuleException(false));
  }
  else if (!this._flushing)
  {
    this.Flush();
    this._ended = true;
    if (this._context.ApplicationInstance != null) {
      this._context.ApplicationInstance.CompleteRequest();
    }
  }
}

在 Page_Load 方法中使用断点调试一个相当简单的 Web 应用程序,我可以看到调用堆栈包括以下行:

System.Web.dll! System.Web.HttpApplication.ExecuteStep(System.Web.HttpApplication.IExecutionStep step = {System.Web.HttpApplication.CallHandlerExecutionStep}, ref bool completedSynchronously = true) + 0x4c bytes



反射(reflect)到 CallHandlerExecutionStep我可以看到属性 IsCancellable定义为:
bool HttpApplication.IExecutionStep.IsCancellable {
  get {
    return !(this._application.Context.Handler is IHttpAsyncHandler);
  }
}

.aspx 页面的默认处理程序是 PageHandlerFactory 的输出。实现 IHttpHandler,而不是 IHttpAsyncHandler - 这将导致 IsCancellable返回 true (就像在我的测试应用程序中所做的那样)。

您是否在根 web.config 或堆栈中配置了不同的 HttpHandler 以使用异步处理程序?例如,您是否使用带有部分回发的更新面板?

关于c# - Response.End () 不会中止当前线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2068184/

相关文章:

c# - 在 C# 中,具有局部作用域的对象是否使用堆栈?

c# - C#WinForms-应用安装错误- list 中的引用与下载的程序集的标识不匹配

c# - 使用 Agility Pack 设置外部加载网页的文本框内部文本

c# - 访问 LTO 磁带内存的源代码 (C/C#)

c# - 尝试运行 Fluent NHibernate 教程示例时出现运行时错误

c# - Azure 表存储插入或合并

asp.net - SharePoint 2013 中的 JS 和 CSS 捆绑和缩小 - 这可能吗?

c# - 在 dotnet 的 Controller 中包含自定义类时调用工厂

c# - 用于 HTML 输出的脱离上下文的字符串变量

c# - 跳过基于模型属性之一的模型的所有验证 (ASP.Net Core 2.0)