我正在使用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
Is it required to have “area” routevalue on actionlink if the application was grouped into areas?
解决方法
This StackOverflow answer正确地指出,如果您正在使用区域,则需要提供区域名称.
例如.
Html.ActionLink("Home","Home",new { Area = "" },new { })