我正在使用 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/