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

Clean page redirect for setting redirect inside logical code

, 27 Jun 2005
Rate this:
Please Sign up or sign in to vote.
This article will detail how you can use the OnPreRender event to redirect a page from inside a logical set of code without getting the 'Thread was being aborted' error.

Introduction

This article will detail how you can use the OnPreRender event to redirect a page from inside a logical set of code without getting the 'Thread was being aborted' error.

We start with the code in its error producing state. We are trying to redirect a page before all the code in the page is finished running, causing a thread abort on the page.

try
{
  if(obj != null)
  {
    //do some code

    //throws a 'Thread being aborted error' 
    //because a redirect is being done here
    Response.Redirect("Subscribers.aspx"); 
  }
  else
  {
    //some other code
  }
}
catch(Exception ex)
{
  //exception handling code
}

So to fix this we need to build a base page to inherit from and use the OnPreRender event to actually perform redirects before the page reloads completely, keeping threads from previous server processing from being aborted. We also create a handy page method SetPageRedirect that adds the URL to be redirected to to the session for access on the OnPreRender event.

public class GlobalPage 
{
  protected override void OnPreRender(EventArgs e)
  {
    if(Session[AppConst.PAGE_REDIRECT]!=null)
    {
      string redirectPage = Session["PAGE_REDIRECT"].ToString();
      Session["PAGE_REDIRECT"] = null;
      Response.Redirect(redirectPage);
    }

    base.OnPreRender (e);
  }

  /// <span class="code-SummaryComment"><summary>
</span>
  /// Sets the redirect session variable for redirection on page render
  /// <span class="code-SummaryComment"></summary>
</span>
  /// <span class="code-SummaryComment"><param name="path"></param>
</span>
  public void SetPageRedirect(bool addRootPath, string path)
  {
    if(!path.StartsWith("/") && addRootPath)
      path = RootPath + "/" + path;
    else if (addRootPath)
      path = RootPath + path;
    Session[AppConst.PAGE_REDIRECT] = path;
}

So now we can inherit from this new page and use the functions to avoid getting any error:

try
{
  if(obj != null)
  {
    //do some code

    //no error throw because the redirect is being done 
    //in the OnPreRender event 
    this.SetPageRedirect(false,"RequestAccess.aspx");
  }
  else
  {
    //some other code
  }
}
catch(Exception ex)
{
  //exception handling code
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Christopher G. Lasater
Web Developer
United States United States
Christopher G. Lasater
 
I am also a published author, please check out my book:
ISBN: 1-59822-031-4
Title: Design Patterns
Author:Christopher G. Lasater
More from my web site
Amazon.com


Comments and Discussions

 
GeneralThere's a better way... PinmemberPatrick Heller9-Mar-07 5:20 
GeneralRe: There's a better way... PinmemberChristopher G. Lasater9-Mar-07 5:28 
GeneralRe: There's a better way... PinmemberPatrick Heller9-Mar-07 5:50 
GeneralRe: There's a better way... PinmemberChristopher G. Lasater9-Mar-07 6:05 
GeneralWhy?... PinmemberAspera26-Jul-06 14:07 
GeneralRe: Why?... Pinmemberchris lasater26-Jul-06 18:30 
GeneralRe: Why?... [modified] PinmemberAspera27-Jul-06 4:21 
Question'AppConst'? PinsussParindShah12310-Oct-05 4:11 
AnswerRe: 'AppConst'? PinmemberMatthias Glubrecht1-Dec-05 3:02 
QuestionIs this an appropriate location for redirecting? PinmemberMark Focas27-Jun-05 14:53 
AnswerRe: Is this an appropriate location for redirecting? Pinmemberchris lasater28-Jun-05 5:42 

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
Web03 | 2.8.141223.1 | Last Updated 27 Jun 2005
Article Copyright 2005 by Christopher G. Lasater
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid