Click here to Skip to main content
15,886,199 members
Articles / Web Development / ASP.NET
Tip/Trick

Why not to pass endResponse parameter as true in Response.Redirect method

Rate me:
Please Sign up or sign in to vote.
4.50/5 (2 votes)
15 Dec 2010CPOL 24.7K   2   1

As per MSDN documentation, Redirect method calls Response.End() method which internally tries to raise ThreadAbortException. If the attempt is successful the calling thread gets aborted which has adverse affect on your site performance.


It is advised and recommended to call the method as Response.Redirect(url, false). And then call CompleteRequest method which causes ASP.NET by pass all its events in the HttpPipeline directly jumping to EndRequest method. See HttpPipeline event model


Please note: CompleteRequest allows your page life cycle event to execute. Consider following scenario.

Code Snippet


Line1: try {
Line2: Response.Redirect(url, true);
Line3: }
Line4: catch(ThreadAbortException ex) {
Line5: // Do nothing
Line6: }

In the above snippet if we pass true in Redirect method, it will surely throw a ThreadAbortException. So if we follow the Microsoft recommended solution, we don't need to add try..catch block. Because CompleteRequest method does not throws any exception neither interrupts the ASP.NET Page life cycle events, rather it jumps HttpPipeline events (refer to this[^] link for Http Pipeline event model).

Recommended: Code Snippet


Line1: Response.Redirect(url, false);
Line2: this.Context.ApplicationInstance.CompleteRequest();

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Member 954405021-Aug-13 20:23
Member 954405021-Aug-13 20:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.