使用 helper.RouteUrl 时指定命名空间的语法

发布于 2024-08-26 15:11:25 字数 558 浏览 10 评论 0 原文

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

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

我读过 Phil Haack 的帖子 带有区域的模糊控制器名称

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

例如,

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

我尝试了添加namespace=“mynamespace”的明显方法,但这不起作用,它只是将命名空间添加到了url中。感谢您的任何帮助。

I am using Asp.Net MVC 2 - RC w/ Areas.

I am receiving an ambigious controller name exception due to having same controller name in two different areas.

I've read Phil Haack's post Ambiguous Controller Names With Areas

I can't figure out the syntax when trying to use UrlHelper (I have an extensions class).

e.g.

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

I've tried the obvious of adding namespace="mynamespace" but that didn't work, it just added the namespace to the url. Thanks for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

双马尾 2024-09-02 15:11:25

也许您可以通过使用两条单​​独的路线来解决这个问题。我认为这也是菲尔试图在 '含区域的控制器名称不明确'帖子

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" });
}

Maybe you can work around it by using two separate routes. I think this is also what Phil is trying to demonstrate in the route registration example on the 'Ambiguous Controller Names With Areas' post

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

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

Then you can refer to both routes like this:

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" });
}
浴红衣 2024-09-02 15:11:25

你试过吗

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

did you try

helper.RouteUrl("ARoute", 
    new { controller = "Home", action = "Index", Area = "YourArea" });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文