views:

74

answers:

1

I have a web form such as mysite.com/list.aspx?state=florida&city=miami that I want users to browse using mysite.com/florida/miami/ and I'm using routing to do so. Then instead of using query string parameters, I end up having to use HttpContext.Current.Items[key] to retrieve the values on my list.aspx page. I have included the code below.

I would like to know what the best practices are to unit test this. Also, is there a better way to implement this without changing my code on the list.aspx page?

Code:

Sample of my Global.asax file:

protected void Application_Start(object sender, EventArgs e)  
{  
    RegisterRoutes(RouteTable.Routes);  
}  

public static void RegisterRoutes(RouteCollection routes)  
{  
    routes.Add(new Route("{state}", new CustomRouteHandler("/list.aspx")));  
    routes.Add(new Route("{state}/{city}", new CustomRouteHandler("/list.aspx")));  
}

Sample of the CustomerRouteHandler:

public class CustomRouteHandler : IRouteHandler  
{  
    public CustomRouteHandler(string virtualPath)  
    {  
        this.VirtualPath = virtualPath;  
    }  

    public string VirtualPath { get; private set; }  

    public IHttpHandler GetHttpHandler(RequestContext requestContext)  
    {  
        foreach (var urlParm in requestContext.RouteData.Values)  
        {  
            requestContext.HttpContext.Items[urlParm.Key] = urlParm.Value;  
        }    

        IHttpHandler page = BuildManager.CreateInstanceFromVirtualPath (VirtualPath, typeof(Page)) as IHttpHandler;  

        return page;  
    }  
}
A: 

You could use HttpUnit to test that all the url endpoints work correctly.

Annie