views:

90

answers:

3

I'm looking for a best practice solution that aims to reduce the amount of URLs that are hard-coded in an ASP.NET application.

For example, when viewing a product details screen, performing an edit on these details, and then submitting the changes, the user is redirected back to the product listing screen. Instead of coding the following:

Response.Redirect("~/products/list.aspx?category=books");

I would like to have a solution in place that allows me to do something like this:

Pages.GotoProductList("books");

where Pages is a member of the common base class.

I'm just spit-balling here, and would love to hear any other way in which anyone has managed their application redirects.

EDIT

I ended up creating the following solution: I already had a common base class, to which I added a Pages enum (thanks Mark), with each item having a System.ComponentModel.DescriptionAttribute attribute containing the page's URL:

public enum Pages
{
    [Description("~/secure/default.aspx")]
    Landing,
    [Description("~/secure/modelling/default.aspx")]
    ModellingHome,
    [Description("~/secure/reports/default.aspx")]
    ReportsHome,
    [Description("~/error.aspx")]
    Error
}

Then I created a few overloaded methods to handle different scenarios. I used reflection to get the URL of the page through it's Description attribute, and I pass query-string parameters as an anonymous type (also using reflection to add each property as a query-string parameter):

private string GetEnumDescription(Enum value)
{
    Type type = value.GetType();
    string name = Enum.GetName(type, value);

    if (name != null)
    {
        FieldInfo field = type.GetField(name);
        if (field != null)
        {
            DescriptionAttribute attr = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute;

            if (attr != null)
                return attr.Description;
        }
    }

    return null;
}

protected string GetPageUrl(Enums.Pages target, object variables)
{
    var sb = new StringBuilder();
    sb.Append(UrlHelper.ResolveUrl(Helper.GetEnumDescription(target)));

    if (variables != null)
    {
        sb.Append("?");
        var properties = (variables.GetType()).GetProperties();

        foreach (var property in properties)
            sb.Append(string.Format("{0}={1}&", property.Name, property.GetValue(variables, null)));
    }

    return sb.ToString();
}

protected void GotoPage(Enums.Pages target, object variables, bool useTransfer)
{
    if(useTransfer)
        HttpContext.Current.Server.Transfer(GetPageUrl(target, variables));
    else
        HttpContext.Current.Response.Redirect(GetPageUrl(target, variables));
}

A typical call would then look like so:

GotoPage(Enums.Pages.Landing, new {id = 12, category = "books"});

Comments?

+1  A: 

You have a wealth of options availible, and they all start with creating a mapping dictionary, whereas you can reference a keyword to a hard URL. Whether you chose to store it in a configuration file or database lookup table, your options are endless.

George
+3  A: 

I'd suggest that you derive your own class ("MyPageClass") from the Page class and include this method there:

public class MyPageClass : Page
{
    private const string productListPagePath = "~/products/list.aspx?category=";
    protected void GotoProductList(string category)
    {
         Response.Redirect(productListPagePath + category);
    }
}

Then, in your codebehind, make sure that your page derives from this class:

 public partial class Default : MyPageClass
 {
      ...
 }

within that, you can redirect just by using:

 GotoProductList("Books");

Now, this is a bit limited as is since you'll undoubtedly have a variety of other pages like the ProductList page. You could give each one of them its own method in your page class but this is kind of grody and not smoothly extensible.

I solve a problem kind of like this by keeping a db table with a page name/file name mapping in it (I'm calling external, dynamically added HTML files, not ASPX files so my needs are a bit different but I think the principles apply). Your call would then use either a string or, better yet, an enum to redirect:

 protected void GoToPage(PageTypeEnum pgType, string category)
 {
      //Get the enum-to-page mapping from a table or a dictionary object stored in the Application space on startup
      Response.Redirect(GetPageString(pgType) + category);  // *something* like this
 }

From your page your call would be: GoToPage(enumProductList, "Books");

The nice thing is that the call is to a function defined in an ancestor class (no need to pass around or create manager objects) and the path is pretty obvious (intellisense will limit your ranges if you use an enum).

Good luck!

Mark Brittingham
I ended up doing something similar to this (see edit above), and will mark this as accepted because I used the Pages enum idea. Thanks Mark.
Ravish
+1  A: 

You have a huge number of options available here. Database table or XML file are probably the most commonly used examples.

// Please note i have not included any error handling code.
public class RoutingHelper
{
    private NameValueCollecton routes;

    private void LoadRoutes()
    {
        //Get your routes from db or config file 
        routes = /* what ever your source is*/
    }

    public void RedirectToSection(string section)
    {
        if(routes == null) LoadRoutes();

        Response.Redirect(routes[section]);
    }
}

This is just sample code, and it can be implemented any way you wish. The main question you need to think about is where you want to store the mappings. A simple xml file could do it:

`<mappings>
    <map name="Books" value="/products.aspx/section=books"/>
    ...
</mappings>`

and then just load that into your routes collection.

Kaius