是什么导致'线程被中止'异常随机发生并向浏览器显示HTTP标头和部分HTML? [英] What causes 'Thread was being aborted' exception to happen at random and show the HTTP header and partial HTML to the browser?

查看:88
本文介绍了是什么导致'线程被中止'异常随机发生并向浏览器显示HTTP标头和部分HTML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正在发生的事情偶尔会随机而不是像您期望的那样返回到浏览器,它看起来像这样:

What is happening is occasionally at random instead of the HTML being returned to the browser as you would expect, it looks a little something like this:


线程被中止.HTTP / 1.1 200 OK

Thread was being aborted.HTTP/1.1 200 OK

(标题的其余部分)

...(比如HTML的1/10)

... (like 1/10th of the HTML)

就是这样,他们在浏览器中得到了一堆文字窗口。

That's it, they are literally getting a bunch of text in the browser window.

它不会一直发生,只是随机发生。计算机...根据我的经验总是有一切的理由,总是。那么这里到底发生了什么?

It doesn't happen all the time, just randomly. Computers... in my experience always have a reason for everything, ALWAYS. So what's the heck is going on here?

我搜索了整个解决方案并发现了很多对Response.Redirect()的调用,这似乎可能是罪魁祸首根据我读过的其他问题...

I have searched the entire solution and found quite a few calls to Response.Redirect() which seems like it could be the culprit based on other questions I have read...

这一切都很好但是并没有告诉我为什么它会随机发生

...或者为什么它会将这个奇怪的结果反馈给浏览器而不是我们设置的正常自定义错误页面。如果这确实是造成它的原因,我还没有确定。如果是,我认为我不能简单地添加'false'参数,因为我不知道如果它继续执行当前代码会怎么做。

...or why it would be giving this strange result back to the browser rather than the normal custom error page we have setup. If this is indeed what is causing it, which I have not yet determined. If it is, I don't think I can simply add the 'false' parameter because I don't know what that would do if it kept executing the current code.

推荐答案

当您致电 Response.End (由其他方法调用,如回复.Redirect Server.Transfer )执行线程中止并抛出 ThreadAbortException (中止线程)在ASP.NET中并不例外。您可以捕获此异常,但它将始终在 catch 处理程序中重新抛出。这使它与其他异常类型区别开来,但它有意义,因为你不能阻止线程中止,并且在进程中通过执行finally块来清理堆栈。

When you call Response.End (which is called by other methods like Response.Redirect and Server.Transfer) the executing thread is aborted and a ThreadAbortException is thrown (aborting threads are not exceptional in ASP.NET). You can catch this exception but it will always be rethrown in the catch handler. This sets it apart from other exception types but it makes sense because you should not be able to stop a thread from aborting and in the process clean up the stack by executing finally blocks.

也许您有一些异常处理逻辑,其中 Response.End try 块内被调用,意外的输出是在 catch 块中生成?

Perhaps you have some exception handling logic where Response.End is called inside a try block and the unexpected output is produced in the catch block?

这样的事情(可能更复杂,难以跟踪成熟代码库):

Something like this (probably more convoluted and hard to track in a "mature" code base):

void HandleRequest() {
  try {
    Response.Redirect(...);
  }
  catch (Exception ex) {
    Response.Write(...);
  }
}

如果 Response.Redirect 结尾为 Response.End 抛出 ThreadAbortException 并且 Response.Write 将执行向响应中添加文本。

If the Response.Redirect ends with Response.End a ThreadAbortException is thrown and the Response.Write will execute adding text to the response.

这篇关于是什么导致'线程被中止'异常随机发生并向浏览器显示HTTP标头和部分HTML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆