为什么我会抛出异常,当我运行的Response.Redirect()? [英] Why do I get a thrown exception when I run Response.Redirect()?
问题描述
我学习ASP.NET,并期待在查询字符串。
I am learning ASP.NET and was looking at QueryStrings.
一个我一直在寻找的例子挂钩一个按钮到一个重定向的呼叫:
One of the examples I was looking at hooks a button up to a redirect call:
protected void btnSubmit_Click(object sender, EventArgs e)
{
try
{
//throws ThreadAbortException: "Thread was being aborted"
Response.Redirect("Form2.aspx");
}
catch (Exception Ex)
{
System.Diagnostics.Debug.WriteLine(Ex.Message);
}
}
为什么它抛出一个ThreadAbortException在这里?这是否正常?我应该做些什么呢?例外的是通常不是一件好事,所以,当我看到这我感到震惊。
Why does it throw a ThreadAbortException here? Is that normal? Should I do something about this? Exceptions are generally not a good thing, so I was alarmed when I saw this.
推荐答案
这是设计使然。这知识库文章所描述的行为(也为 Request.End()
和的Server.Transfer()
方法)。
This is by design. This KB article describes the behavior (also for the Request.End()
and Server.Transfer()
methods).
有关的Response.Redirect()
存在过载:
Response.Redirect(String url, bool endResponse)
如果您通过 endResponse = FALSE ,然后在不引发异常(但运行时将继续处理当前请求)。
If you pass endResponse=false, then the exception is not thrown (but the runtime will continue processing the current request).
如果 endResponse = TRUE (或者,如果你使用过载而没有在布尔参数),则抛出异常,并且当前请求将立即被终止。
If endResponse=true (or if you use the overload without the bool argument), the exception is thrown and the current request will immediately be terminated.
这篇关于为什么我会抛出异常,当我运行的Response.Redirect()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!