from:https://blogs.msdn.microsoft.com/tmarq/2009/06/25/correct-use-of-system-web-httpresponse-redirect/

  Try very, very hard to avoid using Response.Redirect(url), instead, use Response.Redirect(url, false).  Response.Redirect(url), after writing a 302 redirect response to the response buffers, calls Response.End.  This is very expensive.  The alternative, Response.Redirect(url, false) is fast, but unlike Response.Redirect(url), the lines of code which follow the call to Response.Redirect(url, false) will be executed.  More on this later, but first, let me tell you about the horrors of Response.End.

  Response.End is a terrible method.  It was added in ASP.NET 1.0 for compatibility with classic ASP.  In classic ASP, this method terminated script processing, so that the lines of script that followed this call never executed.  How do you simulate that in managed code?  The only way is to abort the thread, which raises a ThreadAbortException and is outrageously expensive.  Normally, that’s exactly what Response.End does in ASP.NET, however, if it is called from within an asynchronous handler/module (a.k.a. an asynchronous pipeline event), it will instead perform a synchronous flush of the response buffers (can you say expensive?) and then complete the request by calling Context.ApplicationInstance.CompleteRequest().  Either way, whether you’re calling it from a synchronous handler/module or an asynchronous handler/module, Response.End is horribly expensive, and you should avoid it.

  Ok, so what if you don’t want the lines of code to execute after you redirect?  Well, one way to accomplish this is to call HttpApplication.CompleteRequest(), which is accessible from the HttpContext. e.g., call calling Context.ApplicationInstance.CompleteRequest().  It’s not the same as aborting the thread, which truly does prevent all subsequent lines of code form running.  The lines of code that follow the call to CompleteRequest() will execute, but as soon as the current page or module that calls this completes, the pipeline will jump ahead to the EndRequest event, thereby short circuiting the pipeline.  This is usually all you need.

So to summarize…

BAD:

Response.Redirect(url);

GOOD:

Response.Redirect(url, false);\

Context.ApplicationInstance.CompleteRequest();

  But before I put my pen away, there’s one more common practice I’ve seen that people should be aware of .  In classic mode, calling Response.Redirect(url) from Application_Error without calling Server.ClearError is doubly expensive.  It causes three exceptions to be thrown, and then either raises a ThreadAbortException or does a synchronous flush and completes the response.  And in integrated mode, calling Response.Redirect(url) from Application_Error without calling Server.ClearError does not even work.  Instead, you should use the following code, which performs well in both classic mode and integrated mode.  If you’re going to redirect from Application_Error, you should do it the following way:

GOOD:

void Application_Error(object sender, EventArgs e)

{

   Server.ClearError();

Response.Redirect(url, false);

Context.ApplicationInstance.CompleteRequest();

}

  The behavior difference between classic and integrated mode with respect to calling Redirect from Application_Error without calling Server.ClearError is just due to the different environments.  You might notice that with a default 3.5 install, if you remove the ASP.NET ScriptModule from the IIS <modules> section, you’re suddenly able to call Redirect from Application_Error without calling Server.ClearError.  ScriptModule registers a PreSendRequestHeaders handler.  In integrated mode, the PreSendRequestHeaders event is implemented differently.  As a result, in integrated mode, the exception will be rendered if you try to redirect without clearing the error from Application_Error.  I’ve attached a sample that demonstrates the difference between the two pipelines.  This sample will demonstrate the difference regardless of whether or not ScriptModule is installed.  Just request default.aspx.  Then change the value of demonstratePipelineDifference in global.asax, and request default.aspx again.  Do this in both classic mode and integrated mode, and observe the behavior.

Thanks,

Thomas

sample.zip

Correct use of System.Web.HttpResponse.Redirect的更多相关文章

  1. 解决:无法在发送 HTTP 标头之后进行重定向。 跟踪信息: 在 System.Web.HttpResponse.Redirect(String url, Boolean endResponse, Boolean permanent) 在 System.Web.Mvc.Async.AsyncControllerActionInvoker.<>……

    问题:在MVC的过滤器中验证用户状态时报如下错误:   无法在发送 HTTP 标头之后进行重定向. 跟踪信息:   在 System.Web.HttpResponse.Redirect(String  ...

  2. log4net 中错误 System.Web.HttpException (0x80004005): 文件不存在

    用日志组件,Global 中配置的输出最后一个错误信息,总是出现下面的错误信息: 2014-04-01 14:35:41,757 级别:ERROR 信息:[Exception]:System.Web. ...

  3. System.Web Namespce

    System.Web概述: System.Web是.NET中web应用开发的一个基础类库,定义浏览器与服务器之间的所有操作方法,包括请求输入流(HttpRequest).输出流(HttpRespons ...

  4. 只有在配置文件或 Page 指令中将 enableSessionState 设置为 true 时,才能使用会话状态。还请确保在应用程序配置的 // 节中包括 System.Web.SessionSta

    我直接在父类的构造方法中调用了sessionj结果就报这个错误 搜了好久 让改web.config 可是不起作用 代码如下: public class BasePage:System.Web.UI.P ...

  5. C# Webservice 解决在运行配置文件中指定的扩展时出现异常。 ---> System.Web.HttpException: 超过了最大请求长度问

    摘自: http://blog.csdn.net/gulijiang2008/article/details/4482993 请在服务器端配置 方法一: 在通过WebService处理大数据量数据时出 ...

  6. 只有在配置文件中或 Page 说明会 enableSessionState 至 true 时刻,能够使用会话状态。另外,还要确保应用程序配置 // 段包含 System.Web.SessionSta

    首先,弄清楚我们的目的,我的目标是验证用户登录.那是,Session["userName"]!=null 在ok该 起初,我是这么写的,结果给出,提示如果上述错误标题,在调查的很长 ...

  7. System.Web

    如果 using System.Web:还是调用不出来其中的类,请在引用的位子添加 System.Web  引用,有的版本不自带这个命名空间. 类: HttpResponse类       用于绘画验 ...

  8. System.Web.Mvc.Controller.cs

    ylbtech-System.Web.Mvc.Controller.cs 1.程序集 System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicK ...

  9. System.Web.HttpContext.cs

    ylbtech-System.Web.HttpContext.cs 1.程序集 System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken ...

随机推荐

  1. 用T-sql 实现Oracle Connect by 的功能

    ; with subDepartment as ( select BesonDepartmentID, DepartmentName, ParentBesonDepartmentID, 1 as Hi ...

  2. 重写,重载,super,this,继承

    重写:overwrite/override 子类根据需要对从基类继承来的方法进行重写. 重写方法必须与被重写方法有相同的方法名,参数列表和返回类型. 重写方法不能使用比被重写方法更严格的访问权限. 重 ...

  3. input 标签实现带提示文字的输入框

    方法一:html5配合css3实现带提示文字的输入框(摆脱js): webkit特有的一个css,可以控制里面的文字样式,配合css3的动画效果和伪类,我们就可以很容易做出一个带动画的输入框,在系统登 ...

  4. JavaScript-hash数组for in 函数

    什么是数组:内存中,连续存储多个数据的存储空间,再起一个名字为什么; 为什么:现实存储多个相关数据,都是集中存储,共同一个名字 程序=数据结构+算法 好的数据结构可以极大的提高程序的执行效率 何时使用 ...

  5. 从0开始学Swift笔记整理(一)

    Swift 是一种适用于 iOS 和 OS X 应用的全新编程语言,它建立在最好的 C 和 Objective-C 语言之上,并且没有 C 语言的兼容性限制.Swift 采用安全的编程模式,增加了现代 ...

  6. Python 输出文字带颜色

    格式:\033[显示方式;前景色;背景色m 说明:前景色            背景色           颜色---------------------------------------30    ...

  7. SQLServer 2012之AlwaysOn —— 指定数据同步链路,消除网络抖动导致的提交延迟问题

    事件起因:近期有研发反应,某数据库从08切换到12环境后,不定期出现写操作提交延迟的问题: 事件分析:在排除了系统资源争用等问题后,初步分析可能由于网络抖动导致同步模式alwayson节点经常出现会话 ...

  8. C#通过反射获取上层调用方法信息

    System.Diagnostics.StackFrame frame = ); System.Reflection.MethodBase method = frame.GetMethod(); st ...

  9. C++ Primer学习笔记一

    /* 题目要求把字符串BRGBBGRRGBBGBBBGRRGBGRG按RGB顺序排列,空间复杂度为O(1) */#include<iostream> using namespace std ...

  10. Nginx学习笔记(九) 配置文件详细说明

    配置文件详细说明 工作了几个月要开始做一些后台开发,免不了接触nginx,以前一般只是简单的使用,更多的分析内部模块的具体实现,为了部署需要进一步掌握配置方法. 全局配置信息 #nginx worke ...