HTT presponse.End VS HTT presponse.Close VS HTT presponse.Sup pressContent [英] HttpResponse.End vs HttpResponse.Close vs HttpResponse.SuppressContent

查看:195
本文介绍了HTT presponse.End VS HTT presponse.Close VS HTT presponse.Sup pressContent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个ASPX页面,我想结束在特定点(不是由于错误条件)的反应,这取决于code路径上,所以没有别的送回往下流。
于是很自然使用:

Within an ASPX page, I want to end the response at specific points (not due to an error condition), depending on code path, so that nothing else is sent back down the stream. So naturally used:

Response.End();

这导致一个ThreadAbortException,这是设计

This results in a ThreadAbortException, which is by design.

下面似乎工作,但看起来并不像正确的方法按的这太问题

The following seems to work but does not look like the correct approach as per this SO question:

Response.Flush();
Response.Close();

那么,这个怎么样?

So, how about this?

Response.Flush();
Response.SuppressContent = true

,然后才让页面完全正常。

and then just let the page complete normally.

我可以只处理和吞ThreadAbortException,但我只是想看看是否有什么不妥/陷阱与燮pressContent的方法呢?

I could just handle and swallow the ThreadAbortException, but I just want to find out if there is anything wrong/gotchas with the SuppressContent approach?

编辑:
为了让多一点的例子。说我有一个ASPX页面一息尚存,我就内容类型更改为许多可能性之一。取决于内容类型和情况下,在该code进行给定的点欲prevent被发送到客户端的任何更多的内容。假设后燮pressContent已被设置为true,不存在与任何进一步的服务器端code运行没有问题。我只是不想别的发送到客户端。

To give a bit more of an example. Say I have a ASPX page whereby I may change the content-type to one of a number of possibilities. Depending on the content-type and scenario, at a given point in the code I want to prevent any more content from being sent to the client. Assume after SuppressContent has been set set to true, that there is no issue with any further server-side code running. I just don't want anything else to be sent to the client.

编辑2:
MyPage.aspx - 有一个母版页可能包括标准的内容,页眉,页脚等等等等本页面可以只呈现为一个正常的页面。它也可以只写出来要下载一个(例如)的XML文档。如果如果离开正常写出一个XML文档(在页面加载时确定),它将清除输出中,设置内容类型为XML,编写所有的XML出来,然后,你最终的ASPX页面呈现的是休息上涨了到底 - 这显然是不要求/断XML。

Edit 2: MyPage.aspx - has a master page which may include standard content, headers, footers etc etc. This page can just render as a normal page. It also can just write out an (e.g.) XML document to be downloaded. If writing out an XML document (determined on page load), it will clear the ouput, set the content-type to XML, write all the XML out and then if left normally, you end up with the rest of the ASPX page rendering being tacked on to the end - that is obviously not required/breaks the XML.

修改3:
现在我使用的是燮pressContent方法。
要尝试并得出了这个问题结束,我养的赏金,并会提出这个问题的另一种方式:当的的使用燮pressContent?为什么你会用它代替到Response.End?

Edit 3: For now I'm using the SuppressContent approach. To try and draw this question to a close, I'm raising a bounty and will put the question another way: When should you use SuppressContent? Why would you use it instead of Response.End?

请参阅我下面的我居然结束了,因为我终于找到了一种方法使用到Response.End时避免ThreadAbortException解决方案提供了答案。我已经通过除外这一点答案。

推荐答案

更新 - 警告:有一个更好的方法,不使用此,请参阅Ethan的答案,而不是

我不会说是有正当理由,以避免到Response.End。想要避免ThreadAbortException的成本,通过让页面请求循环下去,并把它做额外的工作是没有必要的看起来不正确。

I wouldn't say there is a valid reason to avoid the Response.End. Wanting to avoid the cost of the ThreadAbortException, by letting the page request cycle go on and have it do extra work that isn't necessary doesn't seem right.

ThreadAbortException是一种特殊类型的意思是停止执行的线程异常(它会自动重新抛出即使抓到)。这就是说,有一些场景中它可以做伤害(见社区内容加在的 ThreadAbortException )。

ThreadAbortException is a special type of exception meant to stop a thread from execution (its re-thrown automatically even if caught). That said, there are some scenarios where it could do harm (see community content added at the end of ThreadAbortException).

除非你是在你应该坚持到Response.End这些场景之一。需要注意的是一些用法身边,做一个燮pressContent&安培;到Response.End,我猜你想避免一些东西,将来自内部Response.Flush情况。

Unless you are in one of those scenarios you should stick to Response.End. Note that some usages around, do a SuppressContent & Response.End, I guess in cases that you want to avoid some stuff that would come from the internal Response.Flush.

这篇关于HTT presponse.End VS HTT presponse.Close VS HTT presponse.Sup pressContent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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