Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5840655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T11:46:46+00:00 2026-05-22T11:46:46+00:00

I have a simple Session_Start code that looks like this: Sub Session_Start(ByVal sender As

  • 0

I have a simple Session_Start code that looks like this:

Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
    Dim sid = Session.SessionID
    Response.Redirect("~/Blog.aspx")
    dim dummy=4/0
End Sub

It does not work as expected. Usually in my whole site, whenever Response.Redirect() is called, it also terminates the code execution. Whereas here, even if the page eventually redirects, the dim dummy=4/0 line is also executed.

This is causing me problems in other code called from Session_Start() where I built on the assumption that the redirect is an exit point.

I also tried setting endResponse in the Response.Redirect(url, endResponse) overloaded method as true or false but this doesn’t work either.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T11:46:46+00:00Added an answer on May 22, 2026 at 11:46 am

    Having had a dig into the framework source code I can explain why Response.Redirect(url, true) continues to execute code after being called in Session_Start() but not in regular code behind.

    Response.Redirect() ultimately calls an internal overloaded method of Redirect():

    internal void Redirect(string url, bool endResponse, bool permanent)
    {
      // Miscellaneous goings on
    
      if (endResponse)
      {
        this.End();
      }
    }
    

    At the end of this method, if endResponse is true then Response.End() is called. When we look at Response.End() we see the following code:

    public void End()
    {
        if (this._context.IsInCancellablePeriod)
        {
            InternalSecurityPermissions.ControlThread.Assert();
            Thread.CurrentThread.Abort(new HttpApplication.CancelModuleException(false));
        }
        else if (!this._flushing)
        {
            this.Flush();
            this._ended = true;
            if (this._context.ApplicationInstance != null)
            {
                this._context.ApplicationInstance.CompleteRequest();
            }
        }
    }
    

    The method examines the state of the current context’s IsInCancellablePeriod value. This value is internal but we can see it in our debugger:

    If we set a breakpoint inside Session_Start() and examine the current context’s IsInCancellablePeriod non-visible member we see:

    enter image description here

    This means that the request’s thread won’t be aborted and so the code after Response.Redirect() will execute, regardless of whether you set endResponse or not.

    If we set a breakpoint inside an ASPX page’s Page_Load() event we see something different:

    enter image description here

    The current context’s IsInCancellablePeriod non-visible member is set to true and so Thread.CurrentThread.Abort() will be called and no more code will execute after the Response.Redirect().

    The reason for this difference in behaviour is I suspect to do with protecting your session state integrity:

    Don’t redirect after setting a Session variable (or do it right)

    If you need to prevent code from executing after a Response.Redirect() in Session_Start() then you’ll need to use an If...Then...Else:

    If <some_condition_we_have_to_redirect_for> Then
        Response.Redirect("~/Blog.aspx")
    Else
        // Normal session start code goes here
    End If
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.