65.9K
CodeProject is changing. Read more.
Home

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

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.50/5 (2 votes)

Nov 19, 2010

CPOL
viewsIcon

24924

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();