Click here to Skip to main content
Click here to Skip to main content

Tagged as

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

, 15 Dec 2010 CPOL
Rate this:
Please Sign up or sign in to vote.

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)

Share

About the Author

M.Nadeem Shaikh

United States United States
No Biography provided

Comments and Discussions

 
GeneralMy vote of 5 PinmemberMember 954405021-Aug-13 21:23 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150414.1 | Last Updated 15 Dec 2010
Article Copyright 2010 by M.Nadeem Shaikh
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid