为什么 ASP.NET MVC 返回与控制器匹配的多个类型?

Why is ASP.NET MVC returning multiple types matching a controller?

我正在尝试使用 ASP.NET MVC4 设置版本控制 API。 Global.asax.cs 使用 System.Web.Http.GlobalConfiguration.Configuration 在我的项目 WebApiConfig.Register 中调用一个方法。 WebApiConfig.Register 的代码如下所示:

public static void Register(HttpConfiguration config)
{
    CreateRoute(
        routes: config.Routes,
        name: "Root",
        routeTemplate: "api",
        defaults: new { },
        constraints: new { },
        namespaces: new[] { "MyApp.Controllers.Api" },
    );

    CreateRoute(
        routes: config.Routes,
        name: "API",
        routeTemplate: "api/{controller}/{action}",
        defaults: new { action = "Get" },
        constraints: new { },
        namespaces: new[] { "MyApp.Controllers.Api" },
    );

    CreateRoute(
        routes: config.Routes,
        name: "API v2",
        routeTemplate: "api/v2/{controller}/{action}",
        defaults: new { action = "Get" },
        constraints: new { },
        namespaces: new[] { "MyApp.Controllers.Api.v2" },
    );
}

private static void CreateRoute(HttpRouteCollection routes, string name, string routeTemplate, object defaults, object constraints, string[] namespaces)
{
    var defaultsDictionary = new HttpRouteValueDictionary(defaults);
    var constraintsDictionary = new HttpRouteValueDictionary(constraints);
    var dataTokensDictionary = new HttpRouteValueDictionary(new { Namespaces = namespaces, UseNamespaceFallback = false });
    var route = routes.CreateRoute(routeTemplate, defaultsDictionary, constraintsDictionary, dataTokensDictionary);
    routes.Add(name, route);
}

我有两个名为 UsersController 的控制器,一个在 MyApp.Controllers.Api.UsersController,一个在 MyApp.Controllers.Api.v2.UsersController。当我发出像 POST /api/users/Login 这样的请求时,我得到了响应 500 Multiple types were found that match the controller names 'users'. This can happen if the route that services this request ('api/{controller}/{action}') found multiple controllers defined with the same name but differing namespaces, which is not supported.\r\n\r\nThe request for 'users' has found the following matching controllers:\r\nMyApp.Controllers.Api.v2.UsersController\r\nMyApp.Controllers.Api.UsersController

如您所见,我专门在路由上设置了名称空间,并且将 UseNamespaceFallback 设置为 false 以避免冲突,但它们还是会发生。我该如何解决这个问题?

MyApp.Controllers.Api.v2.UsersController 在命名空间 MyApp.Controllers.Api 中。所以在同一个命名空间中有两个UsersController

使用UseNamespaceFallback只是意味着如果v2命名空间中没有UsersController,检查以前的命名空间所以设置它的值对非[=]没有影响17=] namespace, 有问题的namespace.

您应该能够将第一个命名空间更新为:

MyApp.Controllers.Api.v1.UsersController
// and then
MyApp.Controllers.Api.v2.UsersController

然后你可以更新命名空间参数:

namespaces: new[] { "MyApp.Controllers.Api.v1" },

我不知道为什么没有在更多地方提到这个,但显然 System.Web.Http.Dispatcher.DefaultHttpControllerSelector (which, as the name suggests, resolves controllers by default) doesn't support the Namespaces data token (or any other way of restricting namespace). I was able to fix my routing by adding a Nuget reference to WebApiContrib and configuring my API to use NamespaceHttpControllerSelectorGlobal.asax.cs:

GlobalConfiguration.Configuration.Services.Replace(typeof(IHttpControllerSelector), new NamespaceHttpControllerSelector(GlobalConfiguration.Configuration));