gpt4 book ai didi

html - MVC 中奇怪的路由行为

转载 作者:太空宇宙 更新时间:2023-11-04 13:07:34 24 4
gpt4 key购买 nike

考虑以下边栏:

enter image description here

如果我按下菜单项 Solutions 下的子菜单项 All solutions,我会看到一个有点像这样的网格:

enter image description here

这个网格的 url 是 https://something.com/Solutions。现在假设我想进入菜单项 Partners 和子菜单项 Active,url 将是 https://something.com/Partners/active

我正在使用一个简单的 Html.ActionLink,如下所示:

Html.ActionLink("- Active", "Active", "Partners", null, new { @class = "filter-button" })

问题

按照我的方式使用 Html.ActionLink 将用户导航到 url https://something.com/Partners/active,这应该可行,但它没有。该应用程序没有将我重定向到事件合作伙伴 View ,而是将我重定向回 https://something.com/Solutions(正是我来自的地方)但是使用了这个 url : https://something.com/Solutions?undefined=undefined

如果我再次按下菜单项 Partners 中的 Active 子菜单项,url 会挂到 https://something.com/Solutions? undefined=undefined&undefined=undefined 这将永远持续下去。

我不知道为什么会发生这种情况,也没有任何解决方案。

合作伙伴/活跃

public ActionResult Active()
{
using(var db = new DatabaseContext())
{
var partners = db.Partners.Where(x => x.Active).ToList();

return View(partners);
}
}

路线定义

routes.MapRoute(
name: "ControllerIdActionId",
url: "{controller}/{id}/{action}/{id2}",
defaults: new { id2 = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Solutions",
url: "{controller}/{id}/{action}/",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

更新

查看路由定义后,我注意到从未使用过路由定义解决方案。我现在删除了该路由定义,因此路由定义如下所示:

routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "ControllerIdActionId",
url: "{controller}/{id}/{action}/{id2}",
defaults: new { id2 = UrlParameter.Optional }
);

但这并没有什么不同。我尝试更改这两条路线,以便 ControllerIdActionId 路线将在 Default 之前注册,但这也没有任何效果。

最佳答案

路线的顺序很重要。请更改路线的顺序。将 Default 路由移动到第一个位置并尝试。

ControllerIdActionIdSolutions 路由也需要改进,它们看起来乱七八糟。请尽量遵循一定的模式以避免并发症。

例如 Controller / Action /ID

关于html - MVC 中奇怪的路由行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38561791/

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