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

标签 asp.net-mvc-3 routes areas

我正在使用 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 应用程序/区域时的所有默认值。 IE。
    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?

最佳答案

This StackOverflow answer正确说明您需要提供 Area如果您正在使用区域,请命名。

例如。

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

关于asp.net-mvc-3 - 带有区域的 MVC -- Html.ActionLink 返回错误的 URL/路由?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6132248/

相关文章:

asp.net-mvc-3 - MVC3 的人员选择器

c# - 实体类型 List`1 不是当前上下文模型的一部分

angular - 我如何通过路由在 Angular 中托管文件?

ruby-on-rails - rails 使用! route 的(感叹号)

asp.net-mvc - VB 中的 ASP NET MVC 区域路由/多路由问题

c# - 您如何在 MVC3 中的不同 Action Methods 调用之间保持全局变量的值?

php - 在我的 PHP MVC 框架中路由

asp.net-mvc - ASP.NET MVC 2 RC 领域和共享模型

c# - 维护单个项目区域中的 View 的解决方案

asp.net-mvc-3 - KendoGrid 在 kendoTabStrip 中不起作用