gpt4 book ai didi

asp.net - 路由约束asp.net mvc

转载 作者:行者123 更新时间:2023-12-04 17:22:02 24 4
gpt4 key购买 nike

这是我的路由配置:

routes.MapRoute(
name: "AdsCategories",
url: "Ads/{key}",
defaults: new { controller = "Ads", action = "Index" },
//constraints: new { key = },
namespaces: new string[] { "Vprok.Controllers" }
);

routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new string[] { "Vprok.Controllers"}
);

如何创建约束“AdsCategories”?
如果 Controller “Ads” == {key} 中的操作,我需要使用默认路由。

最佳答案

所以基本上如果 {key}是您希望由默认路由处理的现有操作,而不是使用 IndexAds 的操作 Controller ?

为了实现这一点,您可以在关键约束中列出所有可能的操作:

routes.MapRoute(
name: "AdsCategories",
url: "Ads/{key}",
defaults: new { controller = "Ads", action = "Index" },
constraints: new { key = @"^create|update|delete" },
namespaces: new string[] { "Vprok.Controllers" }
);

在这个例子中 AdsCategories仅当 url 不是 ads/create 时,路由才会匹配或 ads/updateads/delete .例如 ads/foobar会匹配。

关于asp.net - 路由约束asp.net mvc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21406388/

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