组合类似的ASP.NET MVC路由

本文关键字:NET MVC 路由 ASP 组合 | 更新日期: 2023-09-27 18:00:51

我有两个非常相似的ASP.NET MVC路由,如下所示。

有没有办法将这两种途径结合起来,并放置一个条件语句,以便(1(对"logs/email/"的任何请求都会转到EmailLogsController,对"logs/user/"的请求会转到UserLogsContrller?

// URL: /areax/logs/email/
// URL: /areax/logs/email/details/51
// URL: /areax/logs/email/details/117
context.MapRouteLowercase(
    "AreaX.Log.Email",
    "areax/logs/email/{action}/{id}",
    new
    {
        controller = "EmailLogs",
        action = "Index",
        id = UrlParameter.Optional
    },
    new[] { ControllerNamespaces.AreaX }
);
// URL: /areax/logs/user/
// URL: /areax/logs/user/details/1
// URL: /areax/logs/user/details/10
context.MapRouteLowercase(
    "AreaX.Log.User",
    "areax/logs/user/{action}/{id}",
    new
    {
        controller = "UserLogs",
        action = "Index",
        id = UrlParameter.Optional
    },
    new[] { ControllerNamespaces.AreaX }
);

就目前情况来看,它们看起来不太干燥。

组合类似的ASP.NET MVC路由

如果您可以重命名控制器(删除日志后缀(,您可以尝试使用{controller}关键字:

    context.MapRouteLowercase(
    "AreaX.Log.Email",
    "areax/logs/{controller}/{action}/{id}",
    new
    {
        action = "Index",
        id = UrlParameter.Optional
    },
    new[] { ControllerNamespaces.AreaX }
);