任何人都知道为什么 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/