1

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

有没有办法将这两条路由结合起来并放置一个条件语句,以便 (1) 对“logs/email/”的任何请求都会发送到 EmailLogsController,而对“logs/user/”的请求会发送到 UserLogsController?

// 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 }
);

就目前而言,它们看起来不是很干燥。

4

1 回答 1

2

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

    context.MapRouteLowercase(
    "AreaX.Log.Email",
    "areax/logs/{controller}/{action}/{id}",
    new
    {
        action = "Index",
        id = UrlParameter.Optional
    },
    new[] { ControllerNamespaces.AreaX }
);
于 2013-02-25T10:51:00.793 回答