Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Response.Redirect behavior

Response.Redirect behavior

Scheduled Pinned Locked Moved ASP.NET
databasehelpquestion
7 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AnalogNerd
    wrote on last edited by
    #1

    Given the following code in an .ASPX webforms application :

    try 
    {
        // a bunch of code, database stuff, etc.
    
        Response.Redirect("somePage.aspx");
    } 
    catch (Exception ex)
    {
       // Error Logging
    }
    finally
    {
        // object cleanup and disposal
    }
    

    I'm wondering if the code in the Finally block (or any code that might be outside of the whole try/catch) would ever be hit. Doesn't the Response.Redirect() immediately redirect making any code that follows useless? Or do I just not understand how this works?

    D L A Z 4 Replies Last reply
    0
    • A AnalogNerd

      Given the following code in an .ASPX webforms application :

      try 
      {
          // a bunch of code, database stuff, etc.
      
          Response.Redirect("somePage.aspx");
      } 
      catch (Exception ex)
      {
         // Error Logging
      }
      finally
      {
          // object cleanup and disposal
      }
      

      I'm wondering if the code in the Finally block (or any code that might be outside of the whole try/catch) would ever be hit. Doesn't the Response.Redirect() immediately redirect making any code that follows useless? Or do I just not understand how this works?

      D Offline
      D Offline
      David Mujica
      wrote on last edited by
      #2

      Not trying to be a smart-ass, but why don't you just try it? Place some dummy code in the finally block and see if it executes. (Use a break-point) My hunch is that the finally code will execute. Also it may depend on which event the Response-Redirect is placed. (PageLoad, PageInit, ButtonClick, etc) Post your results, so we all can learn. :java:

      A 1 Reply Last reply
      0
      • D David Mujica

        Not trying to be a smart-ass, but why don't you just try it? Place some dummy code in the finally block and see if it executes. (Use a break-point) My hunch is that the finally code will execute. Also it may depend on which event the Response-Redirect is placed. (PageLoad, PageInit, ButtonClick, etc) Post your results, so we all can learn. :java:

        A Offline
        A Offline
        AnalogNerd
        wrote on last edited by
        #3

        I'm beyond mortified that I didn't think of that. Especially since after posting the question I ran the code to check something else, but putting a break point there just never occurred to me. :sigh: Ok, so thanks to David's suggestion I have an answer. As it is written that code will throw an exception on the Response.Redirect with the following message:

        Quote:

        {Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.}

        However, it still does the redirect after going through the finally block. Changing the Response.Redirect to

        Response.Redirect("SomePage.aspx", **false**);
        

        will save it from throwing the exception, but will still run the code in the finally and any code after it and then will do the Redirect. Live and learn.

        1 Reply Last reply
        0
        • A AnalogNerd

          Given the following code in an .ASPX webforms application :

          try 
          {
              // a bunch of code, database stuff, etc.
          
              Response.Redirect("somePage.aspx");
          } 
          catch (Exception ex)
          {
             // Error Logging
          }
          finally
          {
              // object cleanup and disposal
          }
          

          I'm wondering if the code in the Finally block (or any code that might be outside of the whole try/catch) would ever be hit. Doesn't the Response.Redirect() immediately redirect making any code that follows useless? Or do I just not understand how this works?

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          As you've already figured out, code in the finally block is guaranteed to execute no matter what (unless the runtime host decides otherwise or a system error forcefully terminates the process itself)

          1 Reply Last reply
          0
          • A AnalogNerd

            Given the following code in an .ASPX webforms application :

            try 
            {
                // a bunch of code, database stuff, etc.
            
                Response.Redirect("somePage.aspx");
            } 
            catch (Exception ex)
            {
               // Error Logging
            }
            finally
            {
                // object cleanup and disposal
            }
            

            I'm wondering if the code in the Finally block (or any code that might be outside of the whole try/catch) would ever be hit. Doesn't the Response.Redirect() immediately redirect making any code that follows useless? Or do I just not understand how this works?

            A Offline
            A Offline
            AmitGajjar
            wrote on last edited by
            #5

            Finally block will always execute, no matter if you redirect your page or throwing some exception. Finally block will not execution on only Some Exceptions[^] When you pass 2nd optional Boolean parameter, it indicate whether you would like to complete page execution or just want to redirect page and leave current execution.

            Thanks -Amit Gajjar (MinterProject)

            1 Reply Last reply
            0
            • A AnalogNerd

              Given the following code in an .ASPX webforms application :

              try 
              {
                  // a bunch of code, database stuff, etc.
              
                  Response.Redirect("somePage.aspx");
              } 
              catch (Exception ex)
              {
                 // Error Logging
              }
              finally
              {
                  // object cleanup and disposal
              }
              

              I'm wondering if the code in the Finally block (or any code that might be outside of the whole try/catch) would ever be hit. Doesn't the Response.Redirect() immediately redirect making any code that follows useless? Or do I just not understand how this works?

              Z Offline
              Z Offline
              ZurdoDev
              wrote on last edited by
              #6

              You already have your answer but to explain Response.Redirect a little more: It is telling the Response object to redirect the user to the new page. What happens is the client gets that message and then requests somePage.aspx. So, it roundtrips but your C# thread will finish. It's essentially just adding a property to the Response object. Server.Transfer will still finish your C# thread (finally will still run) but the client will not roundtrip. Execution will stay on the server and go to somePage.aspx and execute it and send the Response object back to the client.

              There are only 10 types of people in the world, those who understand binary and those who don't.

              A 1 Reply Last reply
              0
              • Z ZurdoDev

                You already have your answer but to explain Response.Redirect a little more: It is telling the Response object to redirect the user to the new page. What happens is the client gets that message and then requests somePage.aspx. So, it roundtrips but your C# thread will finish. It's essentially just adding a property to the Response object. Server.Transfer will still finish your C# thread (finally will still run) but the client will not roundtrip. Execution will stay on the server and go to somePage.aspx and execute it and send the Response object back to the client.

                There are only 10 types of people in the world, those who understand binary and those who don't.

                A Offline
                A Offline
                AnalogNerd
                wrote on last edited by
                #7

                Very well put. Thanks ryanb31.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups