在MVC项目中经常会使用到Area来分开不同的模块让项目结构更加的清晰。

我在一次工作中就遇到了这样一个问题,使用了Area后因为有同样的Controller导致访问的时候会提示有重名的controller,大概提示是路由设置的问题,

解决方案如下。

在每个配置路由的地方加上命名空间。例如:

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 }, // Parameter defaults
                new string[] { "CRM.Controllers" }  //新加的命名空间,用于Area
            );
        }

public override void RegisterArea(AreaRegistrationContext context)
        {
            context.MapRoute(
                "mobile_default",
                "mobile/{controller}/{action}/{id}",
                new { controller = "Home", action = "Index", id = UrlParameter.Optional },
                new string[] { "CRM.Areas.mobile.Controllers" } //用于Area
            );
        }

(注意:必须在每个路由配置里面都加上对应的命名空间)

相关文章:

  • 2021-10-03
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-14
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-07-17
  • 2021-07-25
  • 2021-10-03
  • 2022-12-23
  • 2022-12-23
  • 2021-09-17
  • 2021-10-09
相关资源
相似解决方案