[英]Can “EndResponse” increase performance of ASP.Net page
I have a Response.Redirect
in my Employee page. 我的员工页面中有一个
Response.Redirect
。 It redirects to Salary page. 它重定向到Salary页面。
Response.Redirect ("Salary.aspx");
It was working fine until I added exception handling as below. 它工作正常,直到我添加异常处理如下。
try
{
Response.Redirect ("Salary.aspx");
}
catch(Exception ex)
{
//MyLog();
throw new Exception();
}
//Remaining code in event handler
This caused a new exception saying "Thread was being aborted”. I came to know that this can be avoided by setting endResponse
as false for the redirect. 这引起了一个新的异常,说“线程被中止”。我发现通过将重定向的
endResponse
设置为false可以避免这种情况。
Response.Redirect(url, false);
Context.ApplicationInstance.CompleteRequest();
Explanation of new exception: It always throws the exception but handled by the framework. 新异常的解释:它总是抛出异常但由框架处理。 Since I added a try..catch it was caught there (and I am throwing a new exception)
因为我添加了一个try..catch它被捕到了(我正在抛出一个新的异常)
Note: CompleteRequest
does bypass further HTTP filters and modules, but it doesn't bypass further events in the current page lifecycle 注意:
CompleteRequest
确实绕过了其他HTTP过滤器和模块,但它不会绕过当前页面生命周期中的其他事件
Note: Response.Redirect throw this exception to end processing of the current page. 注意:Response.Redirect将此异常抛出到当前页面的结束处理。 ASP .Net itself handles this exception and calls
ResetAbort
to continue processing. ASP .Net本身处理此异常并调用
ResetAbort
继续处理。
QUESTION 题
PITFALL 陷阱
false
, remaining code in the eventhandler will be executed. false
,则将执行eventhandler中的剩余代码。 So we need to make a if
check for the remaining code (Check: if redirection criteria was not met). if
检查(检查:是否未满足重定向条件)。 Reference 参考
Ending the response ( Response.Redirect(url)
or Response.Redirect(url, true)
) will not have better performance than Response.Redirect(url, false)
. 结束响应(
Response.Redirect(url)
或Response.Redirect(url, true)
) 将没有比Response.Redirect(url, false)
更好的性能。 With false
, since you have control over the code execution, you can simply not execute any more code in the case when you are going to redirect the user. 如果使用
false
,由于您可以控制代码执行,因此在您要重定向用户时,您无法再执行任何代码。
This is specified in the MSDN entry for Response.Redirect()
: 这在
Response.Redirect()
的MSDN条目中指定:
If you specify true for the endResponse parameter, this method calls the End method for the original request, which throws a ThreadAbortException exception when it completes.
如果为endResponse参数指定true,则此方法为原始请求调用End方法,该方法在完成时抛出ThreadAbortException异常。 This exception has a detrimental effect on Web application performance , which is why passing false for the endResponse parameter is recommended.
此异常对Web应用程序性能有不利影响 ,这就是建议为endResponse参数传递false的原因。
You DO need to be concerned about the page lifecycle events, as you noted. 正如您所指出的,您需要关注页面生命周期事件。 You shouldn't continue executing the page events if you are going to Redirect the user ( not only for performance ).
如果要重定向用户( 不仅仅是为了提高性能 ),则不应继续执行页面事件。 I recently wrote a brief example showing what can happen with poor coding/planning if you don't.
我最近写了一个简短的例子, 说明如果不这样做,编码/规划不好会发生什么 。
The bottom line of that post is that Response.Redirect()
returns a 302 to the browser. 该帖子的底线是
Response.Redirect()
向浏览器返回302。 There is a potential for problems when you use Response.Redirect(url, false)
since the page execution continues, and the user can choose to ignore the 302 and instead see the page that would've been rendered... so you need to take steps to ensure they don't see anything you don't want them to see. 有对问题的潜力,当您使用
Response.Redirect(url, false)
因为页继续执行,并且用户可以选择忽略的302,而是看会一直呈现的网页...所以你需要采取措施确保他们看不到任何你不希望他们看到的东西。 The NoRedirect add-on for Firefox is helpful when testing this. Firefox的NoRedirect插件在测试时非常有用。
For best performance: use "false"
as the endResponse
parameter, ensure you aren't running any further code, and ensure the page isn't going to render any information you don't want a user to see if they ignore the 302. 为获得最佳性能:使用
"false"
作为endResponse
参数,确保您没有运行任何其他代码,并确保页面不会呈现您不希望用户忽略302的任何信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.