我正在创建一个商业网站,并且遇到了类别内多个类别的问题。我的动作链接将进入正确的类别,但有时他们会在末尾放置冗余查询字符串。这并不总是会发生,只有当在一个子类别(我知道动作链接试图从任何地方拉路由值,但看到下面)只使用正确的路由URL是错误的。 MVC 3路由。
我已经沉闷了一下,以确保我不会错过什么,但我有同样的问题:
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://本地主机/存储/ T恤/男装/ 2)导航相同的链接将是:
http://localhost/store/tshirt/1?tier2category=men
现在是什么使这更令人困惑的是,当我调试扩展上述方法,在这种情况下,动作链接返回是“http:// localhost/store/tshirt/1”即正确!所以似乎有一些事情发生在MvcHtmlString从扩展到吐出html的时候返回。还应该注意的是,链接仍然会到正确的位置。
任何帮助将是巨大的,因为我还挺拉我的头发在这里...
如果它是任何使用问题上,其中_layout包含部分视图tier1Category索引页面视图发生;包含所有类别的导航视图。 根据URL字符串中的id我最初使用你的方法,我决定去其他路由,因为我觉得linq查询有点矫枉过正: –
当检索类别时。即从tier1Category钻取到tierXCategory。我同意它看起来更清洁,这是我的一个抱怨。 –