MVC 与区域 - Html.ActionLink 返回错误的 URL/路线?

发布于 2024-11-10 08:30:27 字数 1680 浏览 0 评论 0原文

我正在使用 MVC3 并在我的应用程序中有区域。一般来说,一切正常,我可以像这样导航到我的区域(例如 Admin=Area,Controller=Department):

<%: Html.ActionLink("Departments", "DepartmentIndex", "Department", new { area = "Admin"}, null )%>

但是,我注意到,如果我不在 ActionLink 中指定区域,例如,

<%: Html.ActionLink("Test", "Index", "Home")%>

这将停止工作如果我已导航到“管理”区域。即我的网址现在是 http://localhost/myproj/Admin/Home/Index 而不是 http://localhost/myproj/Home/Index

关于这里发生的事情的任何想法?

我的路线都是创建MVC应用程序/区域时的默认路线。即

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

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

我的区域注册

    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "Admin_default",
            "Admin/{controller}/{action}/{id}",
            new { action = "Index", id = UrlParameter.Optional }
        );
    }

是设计使然吗? 这篇文章建议使用 RouteLink 而不是 ActionLink: 说使用RouteLink 是否需要如果应用程序被分组为区域,则操作链接上有“区域”路由值吗?

I am using MVC3 and have Areas in my application. In general, everything works fine, I can navigate to my area (e.g. Admin=Area, Controller=Department) like this:

<%: Html.ActionLink("Departments", "DepartmentIndex", "Department", new { area = "Admin"}, null )%>

However, what I noticed is that if I don't specify the area in my ActionLink, e.g.

<%: Html.ActionLink("Test", "Index", "Home")%>

This will stop working if I have navigated to the "Admin" area. i.e. my url is now
http://localhost/myproj/Admin/Home/Index
instead of
http://localhost/myproj/Home/Index

Any ideas on what is going on here?

My routes are all the defaults when creating an MVC app / Areas. i.e.

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

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

And my area registration

    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "Admin_default",
            "Admin/{controller}/{action}/{id}",
            new { action = "Index", id = UrlParameter.Optional }
        );
    }

Is this by design?
This posting suggests using RouteLink instead of ActionLink:
say to use RouteLink
Is it required to have "area" routevalue on actionlink if the application was grouped into areas?

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

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

发布评论

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

评论(1

请帮我爱他 2024-11-17 08:30:27

这个 StackOverflow 答案< /a> 正确地指出,如果您使用区域,则需要提供 Area 名称。

例如。

Html.ActionLink("Home", "Index", "Home", new { Area = "" }, new { })

This StackOverflow answer correctly states that you need to provide the Area name if you're using areas.

eg.

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