[英]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
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
,则检查以前的命名空间,因此设置它的值对非v2
命名空间没有影响,即有问题的命名空间。
您应该能够将第一个命名空间更新为:
MyApp.Controllers.Api.v1.UsersController
// and then
MyApp.Controllers.Api.v2.UsersController
然后您可以更新命名空间参数:
namespaces: new[] { "MyApp.Controllers.Api.v1" },
我不知道为什么没有提到更多的地方,但显然System.Web.Http.Dispatcher.DefaultHttpControllerSelector (顾名思义,默认情况下解析控制器)不支持Namespaces
数据令牌(或任何其他限制命名空间的方式)。 我能够通过向 WebApiContrib 添加 Nuget 引用并将我的API配置为在Global.asax.cs
中使用NamespaceHttpControllerSelector
来修复我的路由:
GlobalConfiguration.Configuration.Services.Replace(typeof(IHttpControllerSelector), new NamespaceHttpControllerSelector(GlobalConfiguration.Configuration));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.