MVC下 Area和Web同名的Controller问题

时间:2024-01-21 17:34:27

错误如下图:

MVC下 Area和Web同名的Controller问题

解决方案:

1:Area下的XXXAreaRegistration 添加:new string[] { "xxx.Areas.xxx.Controllers" }

2:RouteConfig 下添加 namespaces: new string[] { "xxx.Controllers" }

具体如下:

Area中:

   public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional },
new string[] { "WebTest.Areas.Admin.Controllers" }
);
}

RouteConfig 中:

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[] { "WebTest.Controllers" }
);
}