ASP.NET路由问题

时间:2011-04-19 12:28:11

标签: model-view-controller asp.net-mvc-3 routes

如果我去mysite / Catalog,那就打破了。怎么解决呢?

routes.MapRoute(
    "Localization", // Route name
    "{lang}/{controller}/{action}/{id}", // URL with parameters
    new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);

routes.MapRoute(
    "Default", // Route name
    "{controller}/{action}/{id}", // URL with parameters
    new { controller = "Home", action = "Index", id = UrlParameter.Optional} // Parameter defaults
);

routes.MapRoute(
    "Root",
    "",
    new { controller = "Home", action = "Index", id = "" }
);

1 个答案:

答案 0 :(得分:2)

它会匹配你的第一条路线,认为“目录”是“lang”。您需要为本地化创建约束。

以下路线应匹配前缀为任何语言代码的请求(例如en,cs,de或en-US,en-GB ...)

routes.MapRoute("Localization", "{lang}/{controller}/{action}/{id}",
    new { controller = "Home", action = "Index", id = UrlParameter.Optional },
    new { lang = "[a-z]{2}(-[a-z]{2})" }
);
相关问题