MVC 与区域 - Html.ActionLink 返回错误的 URL/路线?
我正在使用 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 是否需要如果应用程序被分组为区域,则操作链接上有“区域”路由值吗?
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 = UrlParameter.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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这个 StackOverflow 答案< /a> 正确地指出,如果您使用区域,则需要提供
Area
名称。例如。
This StackOverflow answer correctly states that you need to provide the
Area
name if you're using areas.eg.