gpt4 book ai didi

asp.net-mvc-3 - 仅使用 URL 的正确路由是错误的。 MVC 3 路由。

转载 作者:行者123 更新时间:2023-12-04 07:38:41 24 4
gpt4 key购买 nike

我正在创建一个商业网站,但我遇到了类别中包含多个类别的问题。我的 ActionLinks 将转到正确的类别,但有时它们会在末尾放置一个冗余的查询字符串。这并不总是发生,只有在子类别中时才会发生(我知道操作链接试图从任何地方提取路由值,但见下文)

我把它简化了一点以确保我没有遗漏任何东西,但我遇到了同样的问题:

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 似乎发生了一些事情。还应该注意的是,链接仍然指向正确的位置。

任何帮助都会很棒,因为我有点把我的头发拉到这里......

最佳答案

我创建了一个插入了这个确切代码的项目。当然,我用代码替换了助手中的点以填充缺失的变量。生成的 url 完全按照您希望的方式显示,而不是查询字符串。所以在那个音符上,谁知道呢。

至于手头的问题,我觉得您在 url 中包含 categoryId 有点奇怪。这不是类别字符串应该处理的吗?如果是这种情况,您可以使用单一商店路线来收紧一些东西:

routes.MapRoute(
"Store",
"store/{tier1Category}/{tier2Category}",
new { controller = "Store", action = "Index", tier2Category = UrlParameter.Optional }
);

现在您无需将 url 中的信息加倍,您可以使用末尾的数字进行分页或其他有用的操作。我认为这些网址看起来更干净:

http://localhost/store/tshirt
http://localhost/store/tshirt/mens

关于asp.net-mvc-3 - 仅使用 URL 的正确路由是错误的。 MVC 3 路由。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8324433/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com