Response.End() VS Context.ApplicationInstance.CompleteRequest()

The Server.Transfer, Response.Redirect, Response.End methods all raise exceptions. Each of these methods internally call Response.End. The call to Response.End, in turn, causes a ThreadAbortException exception.
https://msdn.microsoft.com/en-us/library/ff647787.aspx#scalenetchapt06_topic22 性能第六章
Server.Transfer, Response.Rediret, Response.End 方法都会产生异常。每种发份都会调用Response.End,产生ThreadAbortedException异常

ThreadAbortException Solution
HttpApplication.CompleteRequest() sets a variable that causes the thread to skip past most of the events in the HttpApplication event pipeline [--] not the Page event chain but the Application event chain.
Response.End and Response.Close are not used in normal request processing when performance is important. Response.End is a convenient, heavy-handed means of terminating request processing with an associated performance penalty. Response.Close is for immediate termination of the HTTP response at the IIS/socket level and causes issues with things like KeepAlive.
The recommended method of ending an ASP.NET request is HttpApplication.CompleteRequest. Keep in mind that ASP.NET rendering will have to be skipped manually since
http://web.archive.org/web/20101224113858/http://www.c6software.com/codesolutions/dotnet/threadabortexception.aspx
HttpApplication.CompleteRequest() 设置的变量能使线程跳过大部分HttpApplication 管道事件,而不是页面事件链,而是Application 事件链
当性能重要时,Response.End 和 Response.Close 不用于正常的请求处理。Response.End 是便捷的重型的方法来终止与性能消耗有关的请求处理.
Response.Close 立即终止IIS/socket级别的HTTP 响应,并导致像 Http 的 KeepAlive问题.
建议终止ASP.NET request方法是HttpApplication.CompleteRequest。 铭记于心的是

1. HttpResponse.End flushes the output buffer to the client and terminates the current request-handling thread , whereas HttpApplication.CompleteRequest tells ASP.NET to immediately skip all future stages in the ASP.NET pipeline and jump directly to the EndRequest step (which also raises the HttpApplication.EndRequest event. The request thread then proceeds with normal end-of-life cleanup.
So, Response.End is like an ejector seat: it quickly ends things, but means you lose control and might be unnecessarily harsh. Whereas CompleteRequest is like making an emergency landing at the nearest airport.

1. Response.End internally throws the ThreadAbortException to kill the request - if you need to do some kind of cleanup, this needs to be done before the call to Response.End is made.
Response.Redirect and Response.End do not interact well with try / catch blocks. So in your situation, you should do all your logic writing to the response stream in your try / catch, then simply call Response.End after your finally block.

1. create pdf
Response.OutputStream.Flush()
Response.OutputStream.Close()
Response.End()

参考:http://weblogs.asp.net/hajan/why-not-to-use-httpresponse-close-and-httpresponse-end

时间: 2024-11-05 14:45:42

Response.End() VS Context.ApplicationInstance.CompleteRequest()的相关文章

Response.End(); 用HttpContext.Current.ApplicationInstance.CompleteRequest 代替

Response.End(); 会报异常 HttpContext.Current.ApplicationInstance.CompleteRequest 这里有个讨论的帖子很有意思:http://q.cnblogs.com/q/32216/

ASP.NET中最保险最环保的返回404的方法

代码如下: Response.StatusCode = 404; Response.SuppressContent = true; Context.ApplicationInstance.CompleteRequest(); 1. 只返回404 Status Code,自定义404页面在IIS的Error Pages中配置. <httpErrors lockAttributes="allowAbsolutePathsWhenDelegated,defaultPath"> &

最保险的网页返回404的方法

对于一个网站来说404错误页面也是非诚重要的,404页面如果做得好的话可以增加用户体验,同时还可以加强用户对网站的友好度.下面分享一下在Asp.net中最保险的404页面的一些设置推荐学习C#入门基础01教程. 代码如下: Response.StatusCode = 404; Response.SuppressContent = true; Context.ApplicationInstance.CompleteRequest(); 1.只返回404 Status Code,自定义404页面在I

如何在try catch中使用Response.End()

在调用Response.End()时,会执行Thread.CurrentThread.Abort()操作. 如果将Response.End()放在try...catch中,catch会捕捉Thread.CurrentThread.Abort()产生的异常System.Threading.ThreadAbortException. 解决方法(任选一个): 1. 在catch中排除ThreadAbortException异常,示例代码如下: try{ Response.End();}catch (S

Response.End报错

以下摘抄自博问:https://q.cnblogs.com/q/31506/       try catch中使用Response.End() 我在WebForm中用ajax发送请求到页面index.aspx,然后我在index.aspx.cs做数据处理,当处理好之后,我用Response.Write()输出我想要的数据,我在index.aspx.cs所作的数据处理是放在一个try---catch中的(因为有访问到数据库),当我输出我想要的数据后,我了Response.End()终止当前页面的运

高效的使用Response.Redirect解决一些不必要的问题(转载)

这篇文章主要介绍了如何高效的使用 Response.Redirect解决一些不必要的问题,需要的朋友可以参考下 介绍: 我正在评估一个 ASP.NET Web 项目应用.它有一些可扩展性问题.意味着当网站访问量增加的时候.系统将会变得缓慢.当我查看应用日志.我找到了大量的 ThreadAbortException. 这个应用大量的使用了 Response.Redirect (是的 endResponse= true),这个就是可扩展性问题的根源.通过endResponse = false 在Re

aspx页面使用ajax遇到try catch中使用Response.End()报错

1.使用Ajax接收数据,在返回Response.Write()后应该调用Response.End()才能将数据写入到调用的页面,才能被jQuery的回调函数获取到返回的JSON数据 2.在try--catch里面不能用Response.End(),否则会报错:由于代码已经过优化或者本机框架位于调用堆栈之上,无法计算表达式的值. 在调用Response.End()时,会执行Thread.CurrentThread.Abort()操作. 如果将Response.End()放在try...catch

高效的使用 Response.Redirect

介绍: 我正在评估一个 ASP.NET Web 项目应用.它有一些可扩展性问题.意味着当网站访问量增加的时候.系统将会变得缓慢.当我查看应用日志.我找到了大量的 ThreadAbortException. 这个应用大量的使用了 Response.Redirect (是的 endResponse= true),这个就是可扩展性问题的根源.通过endResponse = false 在Response.Redirect将会解决这个问题. 但这样做会导致应用程序出现一些奇怪的问题.因为应用程序将假设在

Response.End方法

文章:在try...catch语句中执行Response.End()后如何停止执行catch语句中的内容 调用Response.End()方法能保证,只输出End方法之前的内容. 调用Context.ApplicationInstance.CompleteRequest();方法,会输出方法之后的Response.Write()内容,并且页面的html内容一会被输出.除非你删除掉页面的html,只保留: <%@ Page Language="C#" AutoEventWireup