我的区域在下面.只有有关部分被突出显示.
路线表
public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( "SubFolder",// Route name "SubFolder/ChildController",new { controller = "ChildController",action = "Index" },new[] { "Practise.Areas.SOProblems.Controllers.SubFolder" }); routes.MapRoute( "Default",// Route name "{controller}/{action}",// URL with parameters new { controller = "Home",action = "Index" } // Parameter defaults ); }
This only works when the url is like this localhost:2474/SOProblems/ChildController/index
This does not works when the url is like this localhost:2474/SOProblems/SubFolder/ChildController/index
你能告诉我什么是失踪的吗?
解决方法
This does not works when the url is like this
localhost:2474/SOProblems/SubFolder/ChildController/index
这是正常现象您的路由模式如下所示:SubFolder / ChildController,而不是SubFolder / ChildController / index.除此之外,您在错误的地方定义了路线.您在您的主要路线定义中定义了它,而不是在您的区域路线定义中.所以摆脱主路由的自定义路由定义,并将其添加到SOProblemsAreaRegistration.cs文件(这是您的SOProblems路由应注册的地方):
public override void RegisterArea(AreaRegistrationContext context) { context.MapRoute( "SubFolderRoute","SOProblems/SubFolder/ChildController",new[] { "Practise.Areas.SOProblems.Controllers.SubFolder" } ); context.MapRoute( "SOProblems_default","SOProblems/{controller}/{action}/{id}",new { action = "Index",id = UrlParameter.Optional } ); }
此外,由于您的路由模式(SOProblems / SubFolder / ChildController)无法指定操作名称,因此在此控制器上只能有一个操作,这将是您在这种情况下注册(索引)的默认操作.
如果你想在这个控制器上有更多的动作,而索引是默认的,你应该把它放在你的路由模式中:
context.MapRoute( "SubFolder","SOProblems/SubFolder/ChildController/{action}",new[] { "Practise.Areas.SOProblems.Controllers.SubFolder" } );
在这两种情况下,您的主路由定义可以保留其默认值:
public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( "Default","{controller}/{action}",new { controller = "Home",action = "Index" } ); }