结合类似的ASP.NET MVC路由

时间:2013-02-25 10:43:35

标签: c# asp.net-mvc

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

无论如何要将两个路由组合起来并放置一个条件语句,以便(1)对“logs / email /”的任何请求都转到EmailLogsController,对“logs / user /”的请求转到UserLogsController? / p>

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

目前看来,他们看起来并不是很干。

1 个答案:

答案 0 :(得分:2)

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

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