MVC with Areas -- Html.ActionLink returning wrong URL/Route?
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 = UrlPar开发者_如何学JAVAameter.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?
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 { })
精彩评论