问题描述
|
我正在使用MVC3,并且在我的应用程序中包含Areas。通常,一切正常,我可以这样导航到我的区域(例如Admin = Area,Controller = Department):
<%: Html.ActionLink(\"Departments\",\"DepartmentIndex\",\"Department\",new { area = \"Admin\"},null )%>
但是,我注意到的是,如果我未在ActionLink中指定区域,例如
<%: Html.ActionLink(\"Test\",\"Index\",\"Home\")%>
如果我已导航到\“ Admin \”区域,它将停止工作。即我的网址现在
http:// localhost / myproj / Admin / Home / Index
代替
http:// localhost / myproj / Home /索引
关于这里发生的事情有什么想法吗?
创建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
如果将应用程序分组为区域,是否需要在actionlink上具有“区域”路由值?
解决方法
这个StackOverflow答案正确地指出,如果您正在使用区域,则需要提供the4ѭ名称。
例如。
Html.ActionLink(\"Home\",\"Index\",\"Home\",new { Area = \"\" },new { })