Click here to Skip to main content
Licence 
First Posted 27 Jun 2005
Views 39,638
Bookmarked 16 times

Clean page redirect for setting redirect inside logical code

By Christopher G. Lasater | 27 Jun 2005
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.
1 vote, 33.3%
1
1 vote, 33.3%
2

3

4
1 vote, 33.3%
5
2.67/5 - 3 votes
μ 2.67, σa 3.64 [?]

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

  /// <summary>
  /// Sets the redirect session variable for redirection on page render
  /// </summary>
  /// <param name="path"></param>
  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

About the Author

Christopher G. Lasater

Web Developer

United States United States

Member
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



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralThere's a better way... PinmemberPatrick Heller5:20 9 Mar '07  
GeneralRe: There's a better way... PinmemberChristopher G. Lasater5:28 9 Mar '07  
GeneralRe: There's a better way... PinmemberPatrick Heller5:50 9 Mar '07  
GeneralRe: There's a better way... PinmemberChristopher G. Lasater6:05 9 Mar '07  
GeneralWhy?... PinmemberAspera14:07 26 Jul '06  
GeneralRe: Why?... Pinmemberchris lasater18:30 26 Jul '06  
GeneralRe: Why?... [modified] PinmemberAspera4:21 27 Jul '06  
Question'AppConst'? PinsussParindShah1234:11 10 Oct '05  
AnswerRe: 'AppConst'? PinmemberMatthias Glubrecht3:02 1 Dec '05  
QuestionIs this an appropriate location for redirecting? PinmemberMark Focas14:53 27 Jun '05  
AnswerRe: Is this an appropriate location for redirecting? Pinmemberchris lasater5:42 28 Jun '05  

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.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120209.1 | Last Updated 27 Jun 2005
Article Copyright 2005 by Christopher G. Lasater
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid