MVC区域路由? [英] Mvc area routing?
本文介绍了MVC区域路由?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
区域文件夹看起来像:
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", id = UrlParameter.Optional },
namespaces: new string[] { "MyApp.Areas.Admin.Controllers" });
}
当我这样路由时:http://mydomain.com/User/GetAllUsers
我收到资源未找到错误 (404).将 UserController 添加到 Area 后出现此错误.
When I route like this: http://mydomain.com/User/GetAllUsers
I get resource not found error (404). I get this error after adding UserController to Area.
我该如何解决这个错误?
How can I fix this error?
谢谢...
推荐答案
您弄乱了控制器命名空间.
You've messed up your controller namespaces.
您的主要路线定义应该是:
Your main route definition should be:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new string[] { "MyApp.Controllers" }
);
并且您的管理区域路由注册应该是:
And your Admin area route registration should be:
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" },
new[] { "MyApp.Areas.Admin.Controllers" }
);
}
注意应该如何使用正确的命名空间.
Notice how the correct namespaces should be used.
这篇关于MVC区域路由?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文