3

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

由于在两个不同的区域具有相同的控制器名称,我收到了一个模棱两可的控制器名称异常。

我已经阅读了 Phil Haack 的文章Ambiguous Controller Names With Areas

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

例如

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

我已经尝试了添加 namespace="mynamespace" 的明显方法,但这没有用,它只是将命名空间添加到 url。谢谢你的帮助。

4

2 回答 2

1

你试过了吗

helper.RouteUrl("ARoute", 
    new { controller = "Home", action = "Index", Area = "YourArea" });
于 2010-03-25T02:49:35.613 回答
1

也许您可以通过使用两条单​​独的路线来解决它。我认为这也是 Phil 在“带区域的模糊控制器名称”帖子的路线注册示例中试图展示的内容

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" });
}
于 2010-03-24T23:48:59.137 回答