Skip to content

Instantly share code, notes, and snippets.

@lski
Created August 22, 2014 08:39
Show Gist options
  • Save lski/f93b7ad90186f02dcdc7 to your computer and use it in GitHub Desktop.
Save lski/f93b7ad90186f02dcdc7 to your computer and use it in GitHub Desktop.
To get round the issue of a try/catch/finally being disrupted by transfer and redirection, fill the details of the redirection in this class and run at the end of finally if not null.
/// <summary>
/// To get round the issue of a try/catch/finally being disrupted by transfer and redirection, fill the details of the redirection in this class and run at the end of finally if not null.
/// </summary>
public class Redirection
{
public enum TransferType
{
Redirect,
Transfer
}
public TransferType Type { get; set; }
public String Location { get; set; }
public HttpContextBase Context { get; set; }
public Redirection()
: this(new HttpContextWrapper(HttpContext.Current), "", TransferType.Redirect) {}
public Redirection(HttpContextBase context, String location, TransferType transferType) {
this.Context = context;
this.Type = transferType;
this.Location = location;
}
public void Go()
{
if (Type == TransferType.Transfer)
this.Context.Server.Transfer(this.Location);
else
this.Context.Response.Redirect(this.Location, false);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment