带有区域的MVC - Html.ActionLink返回错误的URL /路由?

Ray*_*ond 6 routes areas asp.net-mvc-3

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

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

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

<%: Html.ActionLink("Test", "Index", "Home")%>
Run Code Online (Sandbox Code Playgroud)

如果我已导航到"管理员"区域,这将停止工作.即我的网址现在是 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" } 
        );
    }
Run Code Online (Sandbox Code Playgroud)

我的区域注册

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

这是设计的吗?这篇文章建议使用RouteLink而不是ActionLink:比如使用RouteLink 如果应用程序被分组到区域,是否需要在actionlink上有"area"routevalue?

Pur*_*ome 8

此StackOverflow应答正确地指出,Area如果您正在使用区域,则需要提供名称.

例如.

Html.ActionLink("Home", "Index", "Home", new { Area = "" }, new { })
Run Code Online (Sandbox Code Playgroud)