1

我无法弄清楚这一点。MVC C#控制器冲突(站点前面的区域管理员)

如何解决问题?

AdminAreaReistration CS

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

RouteConfig

 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 } 
     ); 
    } 

enter image description here

+0

如何正在生成的错误?您是否试图访问特定的网址,或者您是否尝试使用类似“Url.Action”的方式生成网址? –

回答

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)重命名项目名称空间/程序集名称(删除旧的项目名称DLL文件)然后再清理并重新编译),

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

参考文献:

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

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

+0

谢谢。 你是唯一对此问题做出非常清楚回应的人。 :) 我的问题在文章1 –