【发布时间】:2010-06-07 12:06:49
【问题描述】:
我对 ASP.NET MVC 2 下的 URL 生成和路由有一个小问题。我的路由有可选参数,如果指定了参数,则 URL 会正确生成。
因此:
routes.MapRoute("Blog", "Articles/{tag}/{page}", new { controller = "Blog", action = "Index" });
与:
<%: Html.ActionLink(item.Tag, "Index", "Blog", new { tag = item.Tag }, null) %>
正确生成~/Articles/item_tag/1。该链接有效,我的视图已呈现。
我有其他链接,例如:
<%: Html.ActionLink("See more articles", "Index", "Blog") %>
生成~/Blog 而不是~/Articles。
如果我添加第二条路线,例如:
routes.MapRoute("Blog2", "Articles", new { controller = "Blog", action = "Index" });
我的网址已正确呈现。我不明白为什么我需要添加第二条路线,因为它看起来非常多余,因为第一条路线有可选的路段。
任何帮助表示赞赏。
法比安
编辑:添加路线代码。
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" });
【问题讨论】:
标签: asp.net-mvc url routing