【发布时间】:2012-01-09 14:23:36
【问题描述】:
我正在创建一个商务网站,但遇到了类别内多个类别的问题。我的 ActionLink 将进入正确的类别,但有时它们会在末尾放置一个多余的查询字符串。这并不总是发生,只有在子类别中才会发生(我理解 actionlink 试图从任何地方提取路由值,但见下文)
我已经简化了一点,以确保我没有遗漏任何东西,但我遇到了同样的问题:
routes.MapRoute(
"Store1",
"store/{tier1Category}/{tier2Category}/{categoryId}",
new { controller = "Store", action = "Index"},
new { categoryId = @"\d+" }
);
routes.MapRoute(
"Store",
"store/{tier1Category}/{categoryId}",
new { controller = "Store", action = "Index"},
new { categoryId = @"\d+" }
);
我为类别链接创建了一个 HtmlHelper 扩展:
public static MvcHtmlString CategoryLink(this HtmlHelper helper, Category category)
{
.........
return helper.ActionLink(category.Name, "Index", "Store", new { tier1Category = tier1Category, tier2Category = tier2Category, categoryId = category.CategoryID }, null);
}
(截取的代码只是获取tier1Category & tier2Category)
所以在主页上我有一个导航菜单,例如:
http://localhost/store/tshirt/1
现在,如果我要进入该类别的子类别 (http://localhost/store/tshirt/men/2),导航中的相同链接将是:
http://localhost/store/tshirt/1?tier2category=men
现在更令人困惑的是,当我调试上面的扩展方法时,在这种情况下,返回的操作链接是“http://localhost/store/tshirt/1”,即正确!因此,当它吐出 html 时,从扩展返回的 MvcHtmlString 似乎发生了一些事情。另外应该注意的是,链接仍然指向正确的位置。
任何帮助都会很棒,因为我有点把头发拉出来了......
【问题讨论】:
标签: asp.net-mvc-3 model-view-controller