Page view counter

Purpose of the Response.End(); method ?

Last post 01-24-2007 8:02 PM by mbanavige. 13 replies.

Sort Posts:

  • Purpose of the Response.End(); method ?

    01-15-2007, 2:25 PM
    • Loading...
    • brgdotnet
    • Joined on 03-10-2004, 11:59 AM
    • Posts 509
    • Points 965

    What is the purpose of the Response.End() method? Is it necessary to call Response.End() after doing a Response.Redirect() ?

    I am maintaining some C# code. I see that they use the Response.Redirect() method to transfer control to a different web page. Shortly after the Response.Redirect in the code, the Response.End() method is called. I don't know why though?

    Example:

    Response.Redirect(Page.ResolveUrl("~/" + theWebPage));

    Response.End();

     

     

  • Re: Purpose of the Response.End(); method ?

    01-15-2007, 2:33 PM
    • Loading...
    • Snowst
    • Joined on 10-13-2002, 10:40 PM
    • Cape Cod, MA
    • Posts 170
    • Points 760
    My guess without doing the actual test would be that maybe they are trying to avoid the  
     ThreadAbortException 
     This exception normally gets thrown when you redirect to another page. Is it inside of a try, catch loop? Just a guess, not 100% on whether this avoids the exception though. Anyone know for sure?
  • Re: Purpose of the Response.End(); method ?

    01-15-2007, 2:48 PM
    • Loading...
    • Snowst
    • Joined on 10-13-2002, 10:40 PM
    • Cape Cod, MA
    • Posts 170
    • Points 760
    Ok, I couldn't avoid doing the test for my own knowledge, the answer is that Response.End(); is never getting called because the actual Response.Redirect is the one the method thats throwing the exception. So whether you are using at try catch or not, the Response.End(); is never getting called and unnecessary.
  • Re: Purpose of the Response.End(); method ?

    01-15-2007, 2:55 PM
    • Loading...
    • webdevl
    • Joined on 07-06-2003, 8:11 PM
    • Leesburg, VA
    • Posts 38
    • Points 177

    Response.Redirect sends a 302 response header to the browser telling the browser the object has moved.  Response.End ends the page execution.  The thing is that Response.End is called internally by Response.Redirect so there is no need to call it seperately, in fact it will cause an error.  Here are a couple articles that might shed more light on this.

    http://support.microsoft.com/kb/312629

    http://haacked.com/archive/2004/10/06/1308.aspx

  • Re: Purpose of the Response.End(); method ?

    01-15-2007, 6:55 PM
    • Loading...
    • KaziManzurRashid
    • Joined on 03-09-2003, 3:04 PM
    • Dhaka, Bangladesh
    • Posts 882
    • Points 4,782
    To avoid the ThreadAbortException use the overloaded method. Response.Redirect("Target.aspx", true);
    Long Live .NET
    Kazi Manzur Rashid (Amit)
    _________________________
    Web: http //dotnetshoutout.com
    Blog: http://weblogs.asp.net/rashid
    Twitter: http://twitter.com/manzurrashid
    Filed under:
  • Re: Purpose of the Response.End(); method ?

    01-15-2007, 8:41 PM

    hi,

    Response.End()' s behavior has some new behavior in asp.net2.0

    for more information

    refer to this thread

    http://west-wind.com/WebLog/posts/3417.aspx

    Jessica Cao
    Sincerely,
    Microsoft Online Community Support


    “Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread. ”
  • Re: Purpose of the Response.End(); method ?

    01-15-2007, 8:56 PM
    • Loading...
    • satish_nagdev
    • Joined on 09-21-2006, 10:25 AM
    • Mother Earth
    • Posts 1,432
    • Points 6,572

    hi,

    could you tell what error you are getting?

    just comment response.end() and try.

    thanks,

    satish.

    Kind Attn: If a reply to your post helped you, kindly mark it as Answered.
    __________________________________________________
    Please save Animals Help World Society For Protection Of Animals,
    Protect these speechless creatures of GOD
  • Re: Purpose of the Response.End(); method ?

    01-15-2007, 8:57 PM
    • Loading...
    • mbanavige
    • Joined on 11-06-2003, 1:29 PM
    • New England, USA
    • Posts 9,728
    • Points 88,114
    • Moderator
      TrustedFriends-MVPs

    The default behavior of Response.Redirect is to send the redirect to the client and end the response which aborts the thread. 
    in  this example, Response.End will not get called since the thread will automatically abort on the redirect.

    Response.Redirect(Page.ResolveUrl("~/" + theWebPage));
    Response.End(); 

    This behavior can be changed though by using the Redirect overload to instruct that method not to automatically end the response.

     
    
    Response.Redirect("somePage.aspx", False) 'dont end the response just yet
    'now do a little more work
    '...
    Response.End() 'now end the response without completing the rest of the page lifecycle
    
      
    Mike Banavige
    ~~~~~~~~~~~~
    Need a site code sample in a different language? Try converting it with: http://converter.telerik.com/
  • Re: Purpose of the Response.End(); method ?

    01-15-2007, 10:05 PM
    • Loading...
    • Snowst
    • Joined on 10-13-2002, 10:40 PM
    • Cape Cod, MA
    • Posts 170
    • Points 760

    KaziManzurRashid:
    To avoid the ThreadAbortException use the overloaded method. Response.Redirect("Target.aspx", true);

     As explained by mbanavige, in order to avoid the ThreadAbortException you would actually call the overloaded constructor but with a value of false, not true. Where a false tells the Response.Redirect method to not abort the thread (in other words not call Response.End() which is what really is responsible for this exception) This works great, now I can get rid of all of my empty ThreadAbortException Catch Statements!

  • Re: Purpose of the Response.End(); method ?

    01-15-2007, 10:18 PM
    Answer
    • Loading...
    • mbanavige
    • Joined on 11-06-2003, 1:29 PM
    • New England, USA
    • Posts 9,728
    • Points 88,114
    • Moderator
      TrustedFriends-MVPs
    Snowst:

    now I can get rid of all of my empty ThreadAbortException Catch Statements!

    There are 2 side effects to be aware of though which you should consider before making that change.

    1) the page code (full lifecycle) will continue to run consuming system resources.  Imagine how much unnecessary code might run if the redirect occurred in page_load.

    2) after the redirect, its possible that you have some logic (logging perhaps or something...) that would be innapropriate to run if the page wasnt actually sent to the client browser.

    Mike Banavige
    ~~~~~~~~~~~~
    Need a site code sample in a different language? Try converting it with: http://converter.telerik.com/
  • Re: Purpose of the Response.End(); method ?

    01-15-2007, 10:45 PM
    • Loading...
    • Snowst
    • Joined on 10-13-2002, 10:40 PM
    • Cape Cod, MA
    • Posts 170
    • Points 760
    hmm thanks for the drawback considerations, I guess for efficiency and avoiding false exceptions I'll probably have to stick will all of those annoying empty catch statements except for in rare cases. Too bad there's not a way to end a thread without generating this exception, it's a very common annoyance. Most people who call Redirect() would expect for the current thread to abort and not consider it an exception when doing so.
  • Re: Purpose of the Response.End(); method ?

    01-15-2007, 11:09 PM
    Answer
    • Loading...
    • mbanavige
    • Joined on 11-06-2003, 1:29 PM
    • New England, USA
    • Posts 9,728
    • Points 88,114
    • Moderator
      TrustedFriends-MVPs

    since your pondering this one - here's something else to ponder over: http://www.c6software.com/codesolutions/dotnet/threadabortexception.aspx

    happy reading :-)

    Mike Banavige
    ~~~~~~~~~~~~
    Need a site code sample in a different language? Try converting it with: http://converter.telerik.com/
  • Re: Purpose of the Response.End(); method ?

    01-24-2007, 10:17 AM
    • Loading...
    • brgdotnet
    • Joined on 03-10-2004, 11:59 AM
    • Posts 509
    • Points 965

    Thank you for the help. What you say makes perfect sense. And you have made things clearer to me. I need to mentally put a few more pieces of the puzzle together before I understand completely. So I need to ask one or more questions to completely understand this.

    Hm.. Ok here is my question.

    1.) For the following block of code, will line 2 ever be executed? If so, what would ever cause program execution to return to line 2.

    1         Response.Redirect(Page.ResolveUrl("theWebPage.aspx"));
    2         Response.End();

    It seems that the thread of execution at line 1 will end when program execution is transfered to "theWebPage.aspx". So I can't see where code execution will ever return to line 2. Is that the case?

     

  • Re: Purpose of the Response.End(); method ?

    01-24-2007, 8:02 PM
    • Loading...
    • mbanavige
    • Joined on 11-06-2003, 1:29 PM
    • New England, USA
    • Posts 9,728
    • Points 88,114
    • Moderator
      TrustedFriends-MVPs

    Line 2 will not execute.

    since you did not use the overload for Redirect, the thread aborts as soon as the redirect is called.  The only way to reach line 2 is to code it like this:

    1    Response.Redirect(Page.ResolveUrl("theWebPage.aspx"), False)
    2    Response.End()
        
    
     
    Mike Banavige
    ~~~~~~~~~~~~
    Need a site code sample in a different language? Try converting it with: http://converter.telerik.com/
Page 1 of 1 (14 items)