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