Gokhan Turkben
Gokhan Turkben

Reputation: 101

URL Routing Path issue in ASP.net

I have really weird problem with url routing. I defined 3 routes for menu bar like that

routes.MapPageRoute("Article", "Article/{id}/{title}", "~/article.aspx");
routes.MapPageRoute("Contact", "Contact", "~/contact.aspx");            
routes.MapPageRoute("Category","Category/{id}/{name}","~/category.aspx");

when i click to contact i get www.website.com/Contact and then i click to an article i get www.website.com/Article/id/title and all links are working.

However when i click an article firstly and then click contact, i have www.website.com/Article/id/title/contact, or www.website/category/id/name/contact

This problem only occurs when clicking from parameter routes to nonparameter routes. I will be glad if u give any idea. Thank you.

Upvotes: 0

Views: 606

Answers (1)

mason
mason

Reputation: 32694

Your contact link isn't application root relative. You need to make it application root relative. The easiest way in Web Forms is to switch to use a control instead of plain anchor tags.

<asp:HyperLink runat="server" NavigateUrl="~/Contact" Text="Contact" />

Otherwise, when you try to navigate to Contact without making it application root relative, it assumes that Contact is one below the level of the last segment in the current URL (because URL's used to be tied to directories rather than semantic routes, ex: in www.website.com/Article/id/title it thinks title is the directory).

Note, you could also do some inline C# in a plain anchor tag similar to the way you did GetRouteUrl, but I can't remember off the top of my head the correct function call to use.ResolveUrl or ResolveClientUrl or something like that.

Upvotes: 1

Related Questions