使用helper.RouteUrl时指定Namespace的语法

时间:2010-03-24 23:38:17

标签: asp.net-mvc asp.net-mvc-2 asp.net-mvc-routing asp.net-mvc-areas

我正在使用Asp.Net MVC 2 - RC w / Areas。

由于在两个不同的区域拥有相同的控制器名称,我收到一个不明确的控制器名称异常。

我读过Phil Haack的帖子Ambiguous Controller Names With Areas

尝试使用UrlHelper时我无法弄清楚语法(我有一个扩展类)。

e.g。

public static string MyAreaHome(this UrlHelper helper) {
    return helper.RouteUrl("ARoute", 
        new { controller = "Home", action = "Index" });
}

我已经尝试了添加namespace =“mynamespace”这一明显的功能,但是它没有用,只是将命名空间添加到了url中。谢谢你的帮助。

2 个答案:

答案 0 :(得分:1)

也许您可以通过使用两个单独的路线解决它。我认为这也是菲尔试图在“Ambiguous Controller Names With Areas”帖子的路线注册示例中展示的内容

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

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

然后你可以参考这两条路线:

public static string MyAreaHome(this UrlHelper helper) {
    return helper.RouteUrl("ARoute", 
        new { controller = "Home", action = "Index" });
}

public static string MyOtherAreaHome(this UrlHelper helper) {
    return helper.RouteUrl("BRoute", 
        new { controller = "Home", action = "Index" });
}

答案 1 :(得分:1)

你试过

吗?
helper.RouteUrl("ARoute", 
    new { controller = "Home", action = "Index", Area = "YourArea" });
相关问题