如何修复此路由配置?配置路由器返回404

时间:2010-11-13 15:55:04

标签: c# asp.net-mvc routing

我正在尝试定义路由配置,该路由配置将允许以下网址中的可选“区域”,所有这些都将默认为主页:

/uk/home // where the 'uk' parameter can be either 'uk' or 'us'
/uk      // where the 'uk' parameter can be either 'uk' or 'us'
/        // in this case, I just want the region to default to 'uk'

我得到的结果并不理想。第一个(/uk/home)和第三个(/)都有效,但第二个(/uk)返回404。

配置定义为:

        routes.MapRoute(
            null,
            "{region}/{controller}",
            new { region = "^UK|US$" },
            new { controller = "Home", action = "Index" }
            );

        routes.MapRoute(
            null,
            "{region}",
            new { region = "^UK|US$" },
            new { controller = "Home", action = "Index" }
            );

        routes.MapRoute(
            null,
            //"{region}",
            "",
            new {region = "UK", controller = "Home", action = "Index" }
            );

我需要做些什么才能确保所有3个网址都默认为主页,空网址将该区域默认为“uk”?

1 个答案:

答案 0 :(得分:1)

尝试以下路线:

routes.MapRoute(
    "Region",
    "{region}/{controller}",
    new { controller = "Home", action = "Index" },
    new { region = "^UK|US$" }
);

routes.MapRoute(
    "Default",
    "",
    new { controller = "Home", action = "Index", region = "UK" }
);