为什么运行Response.Redirect()时会出现抛出异常?

时间:2022-11-29 20:18:50

I am learning ASP.NET and was looking at QueryStrings.

我正在学习ASP.NET,正在研究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);
        }
    }

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.

为什么会在这里抛出ThreadAbortException?这是正常的吗?我应该为此做点什么吗?例外通常不是一件好事,所以当我看到这一点时我感到震惊。

9 个解决方案

#1


14  

This is by design. This KB article describes the behavior (also for the Request.End() and Server.Transfer() methods).

这是设计的。此知识库文章描述了行为(也适用于Request.End()和Server.Transfer()方法)。

For Response.Redirect() there exists an overload:

对于Response.Redirect(),存在一个重载:

Response.Redirect(String url, bool endResponse)

If you pass endResponse=false, then the exception is not thrown (but the runtime will continue processing the current request).

如果传递endResponse = false,则不抛出异常(但运行时将继续处理当前请求)。

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.

如果endResponse = true(或者如果使用不带bool参数的重载),则抛出异常并立即终止当前请求。

#2


3  

This is normal. Server.Transfer() also throws the same exception.

这个是正常的。 Server.Transfer()也会抛出相同的异常。

This is because internally, both methods call Response.End(), which aborts the current request processing immediately. Rick Strahl has a pretty good blog post that analyzes why there is pretty much nothing you can do to avoid these exceptions.

这是因为在内部,两个方法都调用Response.End(),它立即中止当前请求处理。 Rick Strahl有一篇非常好的博客文章,分析了为什么你几乎没有办法避免这些例外。

#3


3  

Response.Redirect calls Response.End internally, so it throws the exception, use instead :

Response.Redirect在内部调用Response.End,因此抛出异常,改为使用:

Response.Redirect(url, false);

#4


2  

It's normal in that it's what's meant to happen. Basically when the response has been set to a redirection, ASP.NET expects that you're completely done with the request. It aborts the thread as a way of preventing any other processing from occurring (basically it calls Response.End for you, and that throws the exception).

这是正常的,因为这是意味着发生的事情。基本上,当响应设置为重定向时,ASP.NET期望您完全完成请求。它会中止线程,以防止发生任何其他处理(基本上它会为您调用Response.End,并抛出异常)。

It seems to me that it's a bit of an abuse of exceptions, but that's the way it works. You can use the overload which has a second parameter (and pass false) to prevent this from happening if you want to - but if so, make sure that nothing else then tries to write to the response!

在我看来,它有点滥用异常,但这就是它的工作方式。您可以使用具有第二个参数(并传递false)的重载来防止这种情况发生 - 如果您愿意 - 但如果是这样,请确保没有其他任何内容然后尝试写入响应!

#5


1  

I believe you need to follow the instrucitons in this KB article. Response.Redirect calls Response.End(), unless you used the overload specifically made to avoid this behavior. Once the response has been ended, no further operations can happen hence the TA exc.

我相信你需要遵循这篇知识库文章中的说明。 Response.Redirect调用Response.End(),除非您使用专门用于避免此行为的重载。一旦响应结束,就不会发生进一步的操作,因此TA exc。

http://support.microsoft.com/kb/312629

#6


1  

The behavior is by design to support old asp. Here is the link from MS describing what you are experiencing.

该行为是为了支持旧的asp。以下是MS描述您正在体验的内容的链接。

http://support.microsoft.com/default.aspx?scid=kb;EN-US;312629

#7


0  

Calling the overload of Redirect() that just takes a URL also results in a call to Response.End() - which throws a ThreadAbort exception to ensure that no other code after the redirect in your page/UC is run.

调用只接受URL的Redirect()的重载也会导致调用Response.End() - 它会抛出ThreadAbort异常,以确保在页面/ UC中重定向之后没有其他代码运行。

You shouldn't catch this exception .. you should either ignore it in your code or use the overload of Redirect() which takes a boolean that indicates whether to continue processing the request after the redirect.

您不应该捕获此异常..您应该在代码中忽略它或使用Redirect()的重载,它接受一个布尔值,指示在重定向后是否继续处理请求。

#8


0  

Yeah. I code an exit sub after those but I know it's not reached.

是啊。我在那之后编写了一个退出子,但我知道它没有到达。

I suppose if you wanted to you could eat the exception and carry on more stuff afterwords but I wouldn't recommend it.

我想如果你想要你可以吃掉这个例外并继续进行更多的事情,但我不会推荐它。

#9


0  

Already a bunch of reasonable answers here. One other point worth noting though. The Thread Abort Exception is one of those rare exceptions that you can catch and code against, but you can't suppress. It gets automatically re-raised at the end of any try/catch/finally block regardless of what you do.

这里已经有了一堆合理的答案。还有一点值得注意。线程中止异常是您可以捕获和编码的罕见异常之一,但您无法抑制。无论你做什么,它都会在任何try / catch / finally块结束时自动重新引发。

And if you decide to live with this exception, you should ensure that your health monitoring allows for this and doesn't report it as an issue, so you don't get stuck in front of a bunch of non-technical managers trying to explain why your website is throwing so many Thread Abort Exceptions. (I had a manager convinced that all these thread abortions were leaving a mess behind which of course was the complete opposite to the intention of the thread abort.)

如果你决定忍受这个例外,你应该确保你的健康监测允许这个并且不报告它作为一个问题,所以你不要被困在一群试图解释的非技术经理面前为什么你的网站会抛出如此多的线程中止例外。 (我让一位经理确信所有这些线程堕胎都背后一团糟,当然这与线程中止的意图完全相反。)

#1


14  

This is by design. This KB article describes the behavior (also for the Request.End() and Server.Transfer() methods).

这是设计的。此知识库文章描述了行为(也适用于Request.End()和Server.Transfer()方法)。

For Response.Redirect() there exists an overload:

对于Response.Redirect(),存在一个重载:

Response.Redirect(String url, bool endResponse)

If you pass endResponse=false, then the exception is not thrown (but the runtime will continue processing the current request).

如果传递endResponse = false,则不抛出异常(但运行时将继续处理当前请求)。

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.

如果endResponse = true(或者如果使用不带bool参数的重载),则抛出异常并立即终止当前请求。

#2


3  

This is normal. Server.Transfer() also throws the same exception.

这个是正常的。 Server.Transfer()也会抛出相同的异常。

This is because internally, both methods call Response.End(), which aborts the current request processing immediately. Rick Strahl has a pretty good blog post that analyzes why there is pretty much nothing you can do to avoid these exceptions.

这是因为在内部,两个方法都调用Response.End(),它立即中止当前请求处理。 Rick Strahl有一篇非常好的博客文章,分析了为什么你几乎没有办法避免这些例外。

#3


3  

Response.Redirect calls Response.End internally, so it throws the exception, use instead :

Response.Redirect在内部调用Response.End,因此抛出异常,改为使用:

Response.Redirect(url, false);

#4


2  

It's normal in that it's what's meant to happen. Basically when the response has been set to a redirection, ASP.NET expects that you're completely done with the request. It aborts the thread as a way of preventing any other processing from occurring (basically it calls Response.End for you, and that throws the exception).

这是正常的,因为这是意味着发生的事情。基本上,当响应设置为重定向时,ASP.NET期望您完全完成请求。它会中止线程,以防止发生任何其他处理(基本上它会为您调用Response.End,并抛出异常)。

It seems to me that it's a bit of an abuse of exceptions, but that's the way it works. You can use the overload which has a second parameter (and pass false) to prevent this from happening if you want to - but if so, make sure that nothing else then tries to write to the response!

在我看来,它有点滥用异常,但这就是它的工作方式。您可以使用具有第二个参数(并传递false)的重载来防止这种情况发生 - 如果您愿意 - 但如果是这样,请确保没有其他任何内容然后尝试写入响应!

#5


1  

I believe you need to follow the instrucitons in this KB article. Response.Redirect calls Response.End(), unless you used the overload specifically made to avoid this behavior. Once the response has been ended, no further operations can happen hence the TA exc.

我相信你需要遵循这篇知识库文章中的说明。 Response.Redirect调用Response.End(),除非您使用专门用于避免此行为的重载。一旦响应结束,就不会发生进一步的操作,因此TA exc。

http://support.microsoft.com/kb/312629

#6


1  

The behavior is by design to support old asp. Here is the link from MS describing what you are experiencing.

该行为是为了支持旧的asp。以下是MS描述您正在体验的内容的链接。

http://support.microsoft.com/default.aspx?scid=kb;EN-US;312629

#7


0  

Calling the overload of Redirect() that just takes a URL also results in a call to Response.End() - which throws a ThreadAbort exception to ensure that no other code after the redirect in your page/UC is run.

调用只接受URL的Redirect()的重载也会导致调用Response.End() - 它会抛出ThreadAbort异常,以确保在页面/ UC中重定向之后没有其他代码运行。

You shouldn't catch this exception .. you should either ignore it in your code or use the overload of Redirect() which takes a boolean that indicates whether to continue processing the request after the redirect.

您不应该捕获此异常..您应该在代码中忽略它或使用Redirect()的重载,它接受一个布尔值,指示在重定向后是否继续处理请求。

#8


0  

Yeah. I code an exit sub after those but I know it's not reached.

是啊。我在那之后编写了一个退出子,但我知道它没有到达。

I suppose if you wanted to you could eat the exception and carry on more stuff afterwords but I wouldn't recommend it.

我想如果你想要你可以吃掉这个例外并继续进行更多的事情,但我不会推荐它。

#9


0  

Already a bunch of reasonable answers here. One other point worth noting though. The Thread Abort Exception is one of those rare exceptions that you can catch and code against, but you can't suppress. It gets automatically re-raised at the end of any try/catch/finally block regardless of what you do.

这里已经有了一堆合理的答案。还有一点值得注意。线程中止异常是您可以捕获和编码的罕见异常之一,但您无法抑制。无论你做什么,它都会在任何try / catch / finally块结束时自动重新引发。

And if you decide to live with this exception, you should ensure that your health monitoring allows for this and doesn't report it as an issue, so you don't get stuck in front of a bunch of non-technical managers trying to explain why your website is throwing so many Thread Abort Exceptions. (I had a manager convinced that all these thread abortions were leaving a mess behind which of course was the complete opposite to the intention of the thread abort.)

如果你决定忍受这个例外,你应该确保你的健康监测允许这个并且不报告它作为一个问题,所以你不要被困在一群试图解释的非技术经理面前为什么你的网站会抛出如此多的线程中止例外。 (我让一位经理确信所有这些线程堕胎都背后一团糟,当然这与线程中止的意图完全相反。)