IrishChieftain
IrishChieftain

Reputation: 15253

ASP.NET Web Forms Routing Ambiguity

I have routing rules in the following order:

routes.MapPageRoute("Corporate", "Shop/{Category}", "~/Shop.aspx");
routes.MapPageRoute("GiftType", "Shop/{Category}", "~/Shop.aspx");
routes.MapPageRoute("Occasion", "Shop/{Category}", "~/Shop.aspx");
routes.MapPageRoute("Discounted", "Shop/{Category}", "~/Shop.aspx");
routes.MapPageRoute("Featured", "Shop/{Featured}", "~/Shop.aspx");

Links I'm using:

<a href="~/Shop/Corporate" title="Corporate Gifts" runat="server">Corporate</a>
<a href="~/Shop/GiftType" title="Shop by Gift Type" runat="server">Gift Type</a>
<a href="~/Shop/Occasion" title="Shop by Occasion" runat="server">Occasion</a>
<a href="~/Shop/Discounted" title="Discounted" runat="server">Discounted</a>
<a href='<%# "~/Shop/"+Eval("Featured") %>' title='<%# Eval("ProductName") %>'
    runat="server"> 

In the code-behind of the destination page, the following code works and I don't know why. The switch statement is executing for a case value of "True" for "Featured"? It's like I'm not differentiating these routes from each other according to the placeholders?

if (Page.RouteData.Values["Category"] != null)
{
    string category = Page.RouteData.Values["Category"].ToString();

    switch (category)
    {
        case ("Corporate"):
            Response.Write("Corporate");
            break;
        case ("GiftType"):
            Response.Write("GiftType");
            break;
        case ("Occasion"):
            Response.Write("Occasion");
            break;
        case ("Discounted"):
            Response.Write("Discounted");
            break;
        // Do not want the following to be picked up here for Category param
        case ("True"):
            Response.Write("Featured");
            break;           
    }
}

What I want to work code-wise is something like this:

    if (Page.RouteData.Values["Category"] != null)
    {
        string category = Page.RouteData.Values["Category"].ToString();

        switch (category)
        {
            case ("Corporate"):
                Response.Write("Corporate");
                break;
            case ("GiftType"):
                Response.Write("GiftType");
                break;
            case ("Occasion"):
                Response.Write("Occasion");
                break;
            case ("Discounted"):
                Response.Write("Discounted");
                break;          
        }
    }
    else if (Page.RouteData.Values["Featured"].ToString() == "True")
    {
        Response.Write("Featured");
    }

Upvotes: 3

Views: 691

Answers (1)

MikeSmithDev
MikeSmithDev

Reputation: 15797

You only need one route for your categories:

routes.MapPageRoute("shop-categories", "Shop/{Category}", "~/Shop.aspx");

And then you can build the URL like you were, or like

<a id="a1" href="<%$ RouteUrl:Category=Discounted,routename=shop-categories%>" title="Discounted" runat="server">Discounted</a>

I would think you'd want a different route (for clarity's sake) for featured, like

routes.MapPageRoute("featured", "Shop/Featured/{Featured}", "~/Shop.aspx");

and the link (both ways):

<a id="A2" href="<%$ RouteUrl:Featured=False,routename=featured%>" title="Featured" runat="server">Featured False</a>
<a id="A3" href="~/Shop/Featured/True" title="Featured" runat="server">Featured True</a>

Then your code-behind:

if (Page.RouteData.Values["Category"] != null)
{
    //something
}
else if (Page.RouteData.Values["Featured"] != null)
{
    //something
}

And the benefit of building the link like I showed would be if you decide to change your routes... you won't break anything. Let's say you eventually don't like the look of the URL /Shop/Featured/ and just want it to be /Featured/, so you change your route:

routes.MapPageRoute("featured", "Featured/{Featured}", "~/Shop.aspx");

Now in my example above, the link with id="A2" will still work, whereas id="A3" is now broken.

Upvotes: 2

Related Questions