特定命名空间中所有Controller的特定路由

时间:2014-06-11 18:49:02

标签: c# asp.net-mvc

是否可以为特定命名空间中的所有控制器创建特定路由?

例如,我有名称空间“Report”,其中包含Report1,Report2,Report3,...,ReportN。

目前使用默认路线

    routes.MapRoute(
        "Default",
        "{controller}/{action}/{id}",
        new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );

映射是:

localhost:12345/Report1
localhost:12345/Report2
localhost:12345/Report3
...
localhost:12345/ReportN

我想将其更改为:

localhost:12345/Report/Report1
localhost:12345/Report/Report2
localhost:12345/Report/Report3
...
localhost:12345/Report/ReportN

但仅适用于命名空间“Report”

中的Controller

1 个答案:

答案 0 :(得分:1)

添加以下行:

routes.MapRoute(
    "Default",
    "Report/{controller}/{action}/{id}",
    new { controller = "Home", action = "Index", id = UrlParameter.Optional },
    new string[] {"Website.SomeNamespace.Controllers"}
);