asp.net-mvcroutesurl-routingnavigateurl

if i use querystring on a page navigateurl redirecting in a samepage


I am trying to click on a link in products.aspx and redirect to another page categories.aspx. When i use {*id} in routing to handle querystring on products.aspx link is not working well. Sending same products.aspx page.

My Routes:

   routes.MapPageRoute("productsgroup", "products/{groupname}/{*id}", "~/products.aspx");
   routes.MapPageRoute("productscat", "products/brand/{bname}", "~/categories.aspx");

Hyperlink in products.aspx page:

<asp:Hyperlink ID="hyper_link" runat="server" NavigateUrl='<% GetRouteUrl("productscat", new {bname=Eval("brand-name").ToString()})%>' Text="Category1"></asp:Hyperlink>

Hyperlink is in a asp:Repeater and Eval() is working fine on a link and link is seems normal, when i click on the hyperlink, url changes but not sending categories.aspx page.

If i delete querystring {*id} and not use than hyperlink works fine.

I am trying to understand why it's happening and what we can do about it.


Solution

  • There is a simple solution. Change route order and your target url and link will work.

    routes.MapPageRoute("productscat", "products/brand/{bname}", "~/categories.aspx");
    routes.MapPageRoute("productsgroup", "products/{groupname}/{*id}", "~/products.aspx");
    

    Routing thinks target url is "productsgroup" because it knows the url and target url starts like that. Routing waits for the variable and second route like it doesn't exist for routing.

    I hope it helps. Cheers.