Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
286 views
in Technique[技术] by (71.8m points)

c# - Can "EndResponse" increase performance of ASP.Net page

I have a Response.Redirect in my Employee page. It redirects to Salary page.

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.

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)

Note: CompleteRequest does bypass further HTTP filters and modules, but it doesn't bypass further events in the current page lifecycle

Note: Response.Redirect throw this exception to end processing of the current page. ASP .Net itself handles this exception and calls ResetAbort to continue processing.

QUESTION

  1. Whether “setting endResponse as false” can increase performance since the exception is not thrown ?
  2. Whether “setting endResponse as false” can decrease performance since the page lifecycle events are not terminated?

PITFALL

  1. If you set endResponse as false, remaining code in the eventhandler will be executed. So we need to make a if check for the remaining code (Check: if redirection criteria was not met).

Reference

  1. Why Response.Redirect causes System.Threading.ThreadAbortException?
  2. ASP.NET exception "Thread was being aborted" causes method to exit
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Ending the response (Response.Redirect(url) or Response.Redirect(url, true)) will not have better performance than 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.

This is specified in the MSDN entry for Response.Redirect():

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. This exception has a detrimental effect on Web application performance, which is why passing false for the endResponse parameter is recommended.

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. 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. The NoRedirect add-on for Firefox is helpful when testing this.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...