【问题标题】:Controller namespace not routing correctly控制器命名空间未正确路由
【发布时间】:2021-04-06 04:16:04
【问题描述】:

所以我正在使用 MVC 5,并收到此错误。这是因为我有两个区域,它们都有一个同名和同名的控制器。

Multiple types were found that match the controller named 'controller1'. 
This can happen if the route that services this request ('{controller}/{action}/{id}') does not 
specify namespaces to search for a controller that matches the request. 
If this is the case, register this route by calling an overload of 
the 'MapRoute' method that takes a 'namespaces' parameter.

所以我知道我需要在路由中添加名称空间。所以在Route.config.cs我添加了这个

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

然后在global.asaxapplication_start()函数中我添加了ControllerBuilder.Current.DefaultNamespaces.Add("webApp.Controllers");

然后在我的区域,我的错误来自哪里。我有 2 个区域,管理和 myportal。两者都有一个 AreaRegistration.cs 文件。在 AdminAreaRegistration.cs 我添加了这个

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

在 MyPortalAreaRegistration.cs 我添加了这个

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

我在客户端打个电话

 const otherParams = {
        headers: {
            "content-type": "application/json; charset=UTF-8"
        },
        body: JSON.stringify(product),
        method: 'POST'
    };
    console.log(otherParams);
    const response = await fetch('/Item/GetDetailPageURL', otherParams)
        .then(response => {
            response.json();
            console.log(response);
            window.open(response.url, "_blank");
        })
        .then(data => console.log(data));

据我了解,这就是让它工作所需的全部内容,但它仍在抱怨命名空间。我是不是误会/遗漏了什么?

【问题讨论】:

    标签: c# asp.net-mvc routes


    【解决方案1】:

    您不需要更改您的RouteConfig.cs

    您需要做的就是注册您的区域(在区域文件夹中):

    this example 中,区域名称为admin。您需要在adminAreaRegistration.cs 文件夹内的adminAreaRegistration.cs 文件中写入您的区域注册码:

    public class adminAreaRegistration : AreaRegistration 
    {
        public override string AreaName 
        {
            get 
            {
                return "admin";
            }
        }
    
        public override void RegisterArea(AreaRegistrationContext context) 
        {
            context.MapRoute(
                "admin_default",
                "admin/{controller}/{action}/{id}",
                new { action = "Index", id = UrlParameter.Optional }
            );
        }
    }
    

    然后将AreaRegistration.RegisterAllAreas(); 添加到您的Global.asax.cs

    【讨论】:

      【解决方案2】:

      除了area registration 像 Hooman Bahreini 所说的那样,尝试在应用程序启动时设置您的默认名称空间。

      1. 试试这个
      protected void Application_Start()
          {
              AreaRegistration.RegisterAllAreas();
              FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
              RouteConfig.RegisterRoutes(RouteTable.Routes);     
              // in you application start register your default or whatever makes sense Portal Controllers     
        ControllerBuilder.Current.DefaultNamespaces.Add("webApp.Areas.MyPortal.Controllers"); 
          }
      
      1. 相信我 - 转到您的 bin 文件夹并完全手动清除所有旧 dll 的“bin”文件夹。有时会有陈旧的 dll 并且不清理

      2. 您的路线看起来不错,但请在 routes... 上使用此答案仔细检查您的路线

      【讨论】:

        【解决方案3】:

        使用Attribute Routing in ASP.NET MVC 5:

        [RoutePrefix(“reviews”)]
        public class ReviewsController : Controller
        { 
        

        其他地方:

        [RoutePrefix(“otherreviews”)]
        public class ReviewsController : Controller
        { 
        

        【讨论】:

          猜你喜欢
          • 2015-07-05
          • 2012-03-07
          • 1970-01-01
          • 1970-01-01
          • 2012-02-20
          • 2018-01-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多