简体   繁体   中英

How do I unit test routes for web forms?

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

您可以使用HttpUnit来测试所有url端点是否正常工作。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM