【问题标题】:MVC with Areas -- Html.ActionLink returning wrong URL/Route?带有区域的 MVC——Html.ActionLink 返回错误的 URL/路由?
【发布时间】:2011-09-02 04:47:53
【问题描述】:

我正在使用 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 Is it required to have "area" routevalue on actionlink if the application was grouped into areas?

【问题讨论】:

标签: asp.net-mvc-3 routes areas


【解决方案1】:

This StackOverflow answer 正确声明如果您使用区域,则需要提供 Area 名称。

例如。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-08
    • 1970-01-01
    • 2015-07-13
    • 1970-01-01
    • 2013-03-12
    • 1970-01-01
    相关资源
    最近更新 更多