是否可以将一个路由映射到MVC中的一组特定控制器操作?

问题描述 投票:1回答:1

假设我有两组类似的控制器动作:

Photos/OriginalList/{filterBy}
Photos/PhotoShopList/{filterBy}

Photos/OriginalSingle/{id}/{filterBy}
Photos/PhotoShopSingle/{id}/{filterBy}

在这两个组中,filterBy是可选的。

有没有办法创建可以将类似操作组合在一起的路由,还是我需要为每个操作创建显式路由?

我试过这样的东西,但它不起作用:

routes.MapRoute(name: "FilteredPhotoList", url: "Photos/{action=*List}/{filterBy}", defaults: new {
    controller = "Photos",
    action = "OriginalList",
    filterBy = UrlParameter.Optional
}, namespaces: new string[] { "BetterExample" });

routes.MapRoute(name: "FilteredPhotoSingle", url: "Photos/{action=*Single}/{id}/{filterBy}", defaults: new {
    controller = "Photos",
    action = "OriginalSingle",
    filterBy = UrlParameter.Optional
}, namespaces: new string[] { "BetterExample" });
asp.net-mvc routes
1个回答
1
投票

我将明确定义每个路由以获得更好的可读性和代码清洁度。

© www.soinside.com 2019 - 2024. All rights reserved.