区域文件夹看起来像:
Areas Admin Controllers UserController BranchController AdminHomeController
项目目录如下:
Controller UserController GetAllUsers
区域路线登记
public override void Registerarea(AreaRegistrationContext context) { context.MapRoute( "Admin_default","Admin/{controller}/{action}/{id}",new { action = "Index",id = UrlParameter.Optional },new { controller = "Branch|AdminHome|User" } ); }
项目路线注册
public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( name: "Default",url: "{controller}/{action}/{id}",defaults: new { controller = "Home",action = "Index",namespaces: new string[] { "MyApp.Areas.Admin.Controllers" }); }
当我这样路由:http://mydomain.com/User/GetAllUsers我得到资源找不到错误(404).将UserController添加到Area后,会收到此错误.
谢谢…
解决方法
你搞砸了你的控制器名称空间.
您的主要路线定义应为:
routes.MapRoute( name: "Default",namespaces: new string[] { "MyApp.Controllers" } );
您的管理区域路线注册应为:
public override void Registerarea(AreaRegistrationContext context) { context.MapRoute( "Admin_default",new { controller = "Branch|AdminHome|User" },new[] { "MyApp.Areas.Admin.Controllers" } ); }
请注意如何使用正确的命名空间.