gpt4 book ai didi

asp.net-mvc - ASP.Net MVC 路由 : Url only with string ID

转载 作者:行者123 更新时间:2023-12-02 06:19:10 25 4
gpt4 key购买 nike

非常简单的问题,但我找不到答案:

我的 global.asax 中有默认的 {controller}/{action}/{id} 模式。

我还需要一些东西可以给我类似 www.example.com/microsoftwww.example.com/apple 的东西,而 Microsoft 和 apple 是 id 存储在数据库中。使用默认模式为:www.example.com/brands/detail/microsoft

知道图案应该是什么样子吗?我尝试过:{id} 并将 Controller 和操作设置为brandsdetail 它可以满足我的需求,但会破坏所有其他模式。

谢谢

最佳答案

我建议使用一条单独的路由,其约束条件是该 id 不能与您的 Controller 之一匹配,或者必须与数据库中的一个 id 匹配。将其列在默认路由之前,以便在满足条件时首先匹配。

使用简单正则表达式作为固定约束的示例,尽管您可能希望创建一个派生自 IRouteConstraint 的自定义约束来动态限制值。

routes.MapRoute(
"Brands",
"{id}",
new { controller = "brand", action = "detail" },
new { id = "^(Microsoft)|(Apple)$" }
);

您可能想查看http://stephenwalther.com/blog/archive/2008/08/07/asp-net-mvc-tip-30-create-custom-route-constraints.aspx获取更多想法。

关于asp.net-mvc - ASP.Net MVC 路由 : Url only with string ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10239025/

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