【问题标题】:Syntax to specify Namespace when using helper.RouteUrl使用 helper.RouteUrl 时指定命名空间的语法
【发布时间】:2010-03-24 23:38:17
【问题描述】:

我正在使用 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。感谢您的帮助。

【问题讨论】:

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


    【解决方案1】:

    也许您可以通过使用两条单​​独的路线来解决它。我认为这也是 Phil 在“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" });
    }
    

    【讨论】:

    • 这有助于解决问题。在“一般”路线上,我指定了命名空间。在注册路由的区域路由中,我指定了区域命名空间并解决了问题。
    【解决方案2】:

    你试过了吗

    helper.RouteUrl("ARoute", 
        new { controller = "Home", action = "Index", Area = "YourArea" });
    

    【讨论】:

    • 是的,这也不起作用。它将区域作为查询字符串添加到 url
    猜你喜欢
    • 1970-01-01
    • 2021-04-15
    • 1970-01-01
    • 1970-01-01
    • 2011-06-08
    • 1970-01-01
    • 2020-05-04
    • 2019-01-24
    • 1970-01-01
    相关资源
    最近更新 更多