Hi all,
I've a small issue with URL generation & routing under ASP.NET MVC 2. My route has optional parameters and the URL is correctly generated if the parameters are specified.
Thus:
routes.MapRoute("Blog", "Articles/{tag}/{page}", new { controller = "Blog", action = "Index" });
with:
<%: Html.ActionLink(item.Tag, "Index", "Blog", new { tag = item.Tag }, null) %>
does correctly generates ~/Articles/item_tag/1
. The link works, my view is rendered.
I have others link like:
<%: Html.ActionLink("See more articles", "Index", "Blog") %>
that generates ~/Blog
instead of ~/Articles
.
If I add a second route like:
routes.MapRoute("Blog2", "Articles", new { controller = "Blog", action = "Index" });
my URL is correctly rendered. I can't understand why I'd need to add this second route as it seems very redundant because the first route has optional segments.
Any help appreciated.
Fabian
EDIT: added routes code.
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
// Home
routes.MapRoute("Home", "", new { controller = "Home", action = "Index" });
routes.MapRoute("HomeSearch", "Search", new { controller = "Home", action = "Search" });
// Solutions
routes.MapRoute("Solutions", "Solutions", new { controller = "Home", action = "Solutions" });
// Customers
routes.MapRoute("Customers", "Customers", new { controller = "Home", action = "Customers" });
// News
routes.MapRoute("NewsDetails", "News/Details/{id}", new { controller = "News", action = "Details" });
routes.MapRoute("News", "News", new { controller = "News", action = "Index" });
// Articles
routes.MapRoute("BlogDetails", "Articles/Details/{id}", new { controller = "Blog", action = "Details" });
routes.MapRoute("BlogWithTag", "Articles/{tag}/{page}", new { controller = "Blog", action = "Index", tag = "", page = 1 });
routes.MapRoute("Blog", "Articles/{page}", new { controller = "Blog", action = "Index", page = 1 });
// Contact
routes.MapRoute("Contact", "Contact", new { controller = "Contact", action = "Create" });
// Sitemap
routes.MapRoute("Sitemap", "SiteMap", new { controller = "Home", action = "SiteMap" });