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
- Whether “setting endResponse as false” can increase performance since the exception is not thrown ?
- Whether “setting endResponse as false” can decrease performance since the page lifecycle events are not terminated?
PITFALL
- If you set endResponse as
false
, remaining code in the eventhandler will be executed. So we need to make aif
check for the remaining code (Check: if redirection criteria was not met).
Reference