【问题标题】:MVC C# Controller Conflict (site front with area admin)MVC C# 控制器冲突(站点前端与区域管理员)
【发布时间】:2016-11-17 17:26:07
【问题描述】:

我想不通。

如何解决问题?

AdminAreaReistration cs

        public override void RegisterArea(AreaRegistrationContext context) 
    {
        context.MapRoute(
            "CMSAdmin_default",
            "CMSAdmin/{controller}/{action}/{id}",
            new { action = "Index", id = UrlParameter.Optional }
        );
    }

路由配置

        public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Default", action = "Index", id = UrlParameter.Optional }
        );
    }

【问题讨论】:

  • 错误是怎么产生的?您是在尝试访问特定的 URL,还是尝试生成带有 Url.Action 之类的 URL?

标签: asp.net-mvc asp.net-mvc-5 controller asp.net-mvc-areas areas


【解决方案1】:

根据错误图片,在RegisterArea中声明一个区域时可以使用不同的命名空间以避免默认路由和区域路由的命名冲突:

AdminAreaRegistration.cs

public override void RegisterArea(AreaRegistrationContext context) 
{
    context.MapRoute(
        "CMSAdmin_default",
        "CMSAdmin/{controller}/{action}/{id}",
        new { action = "Index", id = UrlParameter.Optional },
        new[] { "cms.site.Areas.CMSAdmin.Controllers" } // Insert area namespace here
    );
}

RouteConfig.cs

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Default", action = "Index", id = UrlParameter.Optional },
        namespaces: new[] { "cms.site.Controllers" } // Insert project namespace here
    );
}

Multiple types were found that match the controller name 错误的可能原因:

1) 在不同区域使用相同的控制器名称(这可能是您当前的问题),

2) 重命名项目命名空间/程序集名称(删除/bin目录中的旧项目名称DLL文件,然后清理并重新构建),

3) 同名但不同版本的引用之间的冲突(删除旧引用然后重构)。

参考资料:

Multiple types were found that match the controller named 'Home'

Having issue with multiple controllers of the same name in my project

【讨论】:

  • 谢谢。你是唯一一个对这个问题反应非常清楚的人。 :) 我在文章 1 中的问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-06
  • 1970-01-01
相关资源
最近更新 更多