Redirecting using strong types in an asp.net web application project

January 13, 2010

Update, I recommend also reading the following post after you read this one if you plan on using this within .Net 4: http://www.lacy.ie/updating-redirection-using-strong-types-to-net-4

It recently began to annoy me that every time I want to redirect to a page I have to use a string. Not a big deal I know but when you have broken links it can reflect badly on you as a developer, so I began to look for some solution that would allow for the following.

  • Refactoring: I need to be able to move a page around without having to find every string reference to it
  • No Reflection: It’s slow I don’t want to have to use it when redirects happen often.
  • Parameters: I want to be able to pass parameters to a function and make that become part of the get request, I want the logic for that to exist on the page being redirected to as that is where the parameters are being used, this will also help with refactoring, the plain ole redirect logic however would just exist on a base class, don’t want to have to rewrite for every page.

I knew the ideal situation would be if I could somehow use the fact that web application projects use the folder hierarchy for its namespace convention and end up being able to redirect to the page ~/Users/UserDetails.aspx?UserId=14 with the following command Users.UserDetails.Redirect(14);

Here is how I did it:

I have a web application project, let’s call it MyProject.Web

I have two pages called UserDetails.aspx and UserList.aspx in the folder Users.

They both inherit from the following base class:

namespace MyProject.UI
{
/// <summary>
/// Summary description for BasePage
/// </summary>
public abstract class BasePage<T>:Page where T : Page
{
public static String GetUrl()
{
return “~/” + typeof(T).ToString().Replace(“MyProject.Web.”, “”).Replace(‘.’, ‘/’) + “.aspx”;
}
public static String GetUrl(String parameters)
{
return GetUrl() + “?” + parameters;
}
public static void Redirect()
{
HttpContext.Current.Response.Redirect(GetUrl());
}
public static void Redirect(String parameters)
{
HttpContext.Current.Response.Redirect(GetUrl(parameters));
}
public static void Redirect(Boolean endResponse)
{
HttpContext.Current.Response.Redirect(GetUrl(),endResponse);
}
public static void Redirect(String parameters,Boolean endResponse)
{
HttpContext.Current.Response.Redirect(GetUrl(parameters), endResponse);
}
}
}

They follow this pattern:

namespace MyProject.Web.Users

{

public partial class UserDetails: BasePage<UserDetails>

{

public static void Redirect(int userId)

{

Redirect(“UserId=” + userId.ToString());

}

}

}

Now because UserDetails and UserList are in the same folder I can simply call UserDetails.Redirect(selectedUserId) on the UserList and UserList.Redirect() on the UserDetails.
The only real disadvantage is that it could confuse people who discover a Redirect method on their current page that they can’t use to make it to another page.
However it should be abundantly clear to everyone what you are doing when they see it already written.
Also every time you move a class, remove or add a parameter, it will break on compile time not at run time.

Comments and feedback welcome.

  • Share/Bookmark

Related posts:

  1. How to Mock the HttpContext in asp.net I find mocking the http context rather difficult, you can’t...
  2. jQuery Grid Plugin (JqGrid) with ASP.Net Web Forms Get JqGrid here: http://www.trirand.com/blog/ So far I’ve seen two implementations...
  3. Handling Relative Paths and debug mode with Jquery in ASP.Net Web Forms I had a problem where I wanted to use the...
  4. Official JqGrid ASP.Net Web Forms Control Alpha Demo First look at the official JqGrid ASP.Net Web Forms control...

Related posts brought to you by Yet Another Related Posts Plugin.

tags: , , , ,
posted in Programming by admin

Follow comments via the RSS Feed | Leave a comment | Trackback URL

Leave Your Comment

 
Powered by Wordpress and MySQL. Theme by Shlomi Noach, openark.org