有什么方法可以更短地编写这些代码吗?我必须编写代码 1,因为代码 2 会覆盖它。
代码 1. 第一个文件夹(控制器名称)不同:如果列表中不存在新控制器名称,我将使用“[{controller}]/{action}/{id}”作为新控制器名称。
routes.MapRoute(name: "All", url: "All/{action}/{id}", 默认值: new { controller = "All", action = "Index", id = UrlParameter.Optional }); routes.MapRoute(name: "Account", url: "Account/{action}/{id}", 默认值: new { controller = "Account", action = "Index", id = UrlParameter.Optional }); routes.MapRoute(name: "AdminRecord", url: "AdminRecord/{action}/{id}", 默认值: new { controller = "AdminRecord", action = "Index", id = UrlParameter.Optional }); routes.MapRoute(name: "Control", url: "Control/{action}/{id}", 默认值: new { controller = "Control", action = "Index", id = UrlParameter.Optional }); routes.MapRoute(name: "ControlTemplate", url: "ControlTemplate/{action}/{id}", 默认值: new { controller = "ControlTemplate", action = "Index", id = UrlParameter.Optional }); routes.MapRoute(name: "Domain", url: "Domain/{action}/{id}", 默认值: new { controller = "Domain", action = "Index", id = UrlParameter.Optional }); routes.MapRoute(name: "FieldTemplate", url: "FieldTemplate/{action}/{id}", 默认值: new { controller = "FieldTemplate", action = "Index", id = UrlParameter.Optional }); routes.MapRoute(name: "Form", url: "Form/{action}/{id}", 默认值: new { controller = "Form", action = "Index", id = UrlParameter.Optional }); routes.MapRoute(name: "Home", url: "Home/{action}/{id}", 默认值: new { controller = "Home", action = "Index", id = UrlParameter.Optional }); routes.MapRoute(name: "Language", url: "Language/{action}/{id}", 默认值: new { controller = "Language", action = "Index", id = UrlParameter.Optional }); routes.MapRoute(name: "LogViewer", url: "LogViewer/{action}/{id}", 默认值: new { controller = "LogViewer", action = "Index", id = UrlParameter.Optional }); routes.MapRoute(name: "PageSlug", url: "PageSlug/{action}/{id}", 默认值: new { controller = "PageSlug", action = "Index", id = UrlParameter.Optional }); routes.MapRoute(name: "PropertyTemplate", url: "PropertyTemplate/{action}/{id}", 默认值: new { controller = "PropertyTemplate", action = "Index", id = UrlParameter.Optional }); routes.MapRoute(name: "RecordType", url: "RecordType/{action}/{id}", 默认值: new { controller = "RecordType", action = "Index", id = UrlParameter.Optional }); routes.MapRoute(name: "Tag", url: "Tag/{action}/{id}", 默认值: new { controller = "Tag", action = "Index", id = UrlParameter.Optional }); routes.MapRoute(name: "Test", url: "Test/{action}/{id}", 默认值: new { controller = "Test", action = "Index", id = UrlParameter.Optional }); // 控制器/动作/ID routes.MapRoute(name: "Default", url: "[{controller}]/{action}/{id}", 默认值: new { action = "Index", id = UrlParameter.Optional });
代码 2. 文件夹数不同:
routes.MapRoute(name: "/Slug/ (Page)", url: "{slug}/", 默认值: new { controller = "PageSlug", action = "Slug" }); routes.MapRoute(name: "/Slug/Slug/ (Page)", url: "{parentSlug1}/{slug}/", 默认值: new { controller = "PageSlug", action = "Slug" }); routes.MapRoute(name: "/Slug/Slug/Slug/ (Page)", url: "{parentSlug2}/{parentSlug1}/{slug}/", 默认值: new { controller = "PageSlug", action = "Slug " }); routes.MapRoute(name: "/Slug/Slug/Slug/Slug/ (Page)", url: "{parentSlug3}/{parentSlug2}/{parentSlug1}/{slug}/", 默认值: new { controller = "PageSlug ", 动作 = "蛞蝓" }); routes.MapRoute(name: "/Slug/Slug/Slug/Slug/Slug/ (Page)", url: "{parentSlug4}/{parentSlug3}/{parentSlug2}/{parentSlug1}/{slug}/", 默认值:新的{控制器=“PageSlug”,动作=“Slug”}); routes.MapRoute(name: "/Slug/Slug/Slug/Slug/Slug/Slug/ (Page)", url: "{parentSlug4}/{parentSlug3}/{parentSlug2}/{parentSlug1}/{slug}/",默认值:新 { 控制器 = "PageSlug",动作 = "Slug" }); routes.MapRoute(name: "/Slug/Slug/Slug/Slug/Slug/Slug/Slug/ (页)", url: "{parentSlug5}/{parentSlug4}/{parentSlug3}/{parentSlug2}/{parentSlug1}/ {slug}/",默认值:new { controller = "PageSlug", action = "Slug" }); routes.MapRoute(name: "/Slug/Slug/Slug/Slug/Slug/Slug/Slug/Slug/ (页)", url: "{parentSlug6}/{parentSlug5}/{parentSlug4}/{parentSlug3}/{parentSlug2 }/{parentSlug1}/{slug}/",默认值:new { controller = "PageSlug", action = "Slug" }); routes.MapRoute(name: "/Slug/Slug/Slug/Slug/Slug/Slug/Slug/Slug/Slug/ (页)", url: "{parentSlug7}/{parentSlug6}/{parentSlug5}/{parentSlug4}/ {parentSlug3}/{parentSlug2}/{parentSlug1}/{slug}/",默认值:new { controller = "PageSlug", action = "Slug" }); routes.MapRoute(name: "/Slug/Slug/Slug/Slug/Slug/Slug/Slug/Slug/Slug/Slug/ (页)", url: "{parentSlug8}/{parentSlug7}/{parentSlug6}/{parentSlug5 }/{parentSlug4}/{parentSlug3}/{parentSlug2}/{parentSlug1}/{slug}/",默认值:new { controller = "PageSlug", action = "Slug" }); routes.MapRoute(name: "/Slug/Slug/Slug/Slug/Slug/Slug/Slug/Slug/Slug/Slug/Slug/ (页)", url: "{parentSlug9}/{parentSlug8}/{parentSlug7}/ {parentSlug6}/{parentSlug5}/{parentSlug4}/{parentSlug3}/{parentSlug2}/{parentSlug1}/{slug}/”,默认值:new { controller = "PageSlug", action = "Slug" });