我正在尝试api versioning
使用header
,但在different folder structure
中如下所示。
在Controller folder
V1
内有CustomerController.cs
个子文件夹
和
在Controller folder
V2
CustomerController.cs
子文件夹中
当我使用URL
上面的用户api版本工作正常。
我的问题是当我使用header
尝试此方法时,它给出了以下错误:
{“消息”:“发生了错误。”,“ExceptionMessage”: “发现多个类型与名为'customer'的控制器相匹配。 如果为此请求提供服务的路由,则会发生这种情况 ('api / {controller} / {id}')发现了多个控制器 同名但不同的命名空间,不受支持。\ r \ n \ r \ n 请求'客户'找到以下匹配项 控制器:\ r \ nApiVersion.Controllers.V1.CustomerController \ r \ nApiVersion.Controllers.V2.CustomerController“,”ExceptionType“:”System.InvalidOperationException“,”StackTrace“:” 在 System.Web.Http.Dispatcher.DefaultHttpControllerSelector.SelectController(HttpRequestMessage 请求)\ r \ n at System.Web.Http.Dispatcher.HttpControllerDispatcher.d__1.MoveNext()” }
WebApiConfig.cs
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
CustomeControllerSelector.cs
public override HttpControllerDescriptor SelectController(HttpRequestMessage request)
{
var controllers = GetControllerMapping();
var routeData = request.GetRouteData();
var controllerName = routeData.Values["controller"].ToString();
//HttpControllerDescriptor controllerDescriptor;
var version = GetVersionFromAcceptHeaderVersion(request);
var controllerKey = String.Format(CultureInfo.InvariantCulture, "{0}.{1}",
version, controllerName);
//if (controllers.TryGetValue(controllerName, out controllerDescriptor))
// {
if (!string.IsNullOrEmpty(version))
{
HttpControllerDescriptor versionedControllerDescriptor;
if (controllers.TryGetValue(controllerKey, out versionedControllerDescriptor))
{
return versionedControllerDescriptor;
}
}
return null;
}
private string GetVersionFromAcceptHeaderVersion(HttpRequestMessage request)
{
var acceptHeader = request.Headers.Accept;
foreach (var mime in acceptHeader)
{
if (mime.MediaType == "application/json" || mime.MediaType == "text/html")
{
var version = mime.Parameters
.Where(v => v.Name.Equals("version", StringComparison.OrdinalIgnoreCase))
.FirstOrDefault();
if (version != null)
{
return version.Value;
}
return string.Empty;
}
}
return string.Empty;
}
答案 0 :(得分:1)
我试过以下。截至目前工作 -
public class CustomControllerSelector : DefaultHttpControllerSelector
{
//const string partName = "Webapi.Controllers";
private readonly HttpConfiguration _config;
public CustomControllerSelector(HttpConfiguration config)
: base(config)
{
_config = config;
}
public override System.Web.Http.Controllers.HttpControllerDescriptor SelectController(HttpRequestMessage request)
{
var _route = request.GetRouteData();
var controllerName = base.GetControllerName(request);
var type = _config.Services.GetAssembliesResolver();
var controlles = _config.Services.GetHttpControllerTypeResolver().GetControllerTypes(type);
object name;
_route.Values.TryGetValue("route", out name);
//No more hard coding
var partName = controllers.FirstOrDefault().Namespace;
var st = name as string;
if (st != null)
{
var conType = controlles.FirstOrDefault(a => a.Namespace == string.Format("{0}.{1}", partName, st));
if (conType != null)
return new System.Web.Http.Controllers.HttpControllerDescriptor(_config, controllerName, conType);
}
return base.SelectController(request);
}
}
在WebApiConfig.cs中 -
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{route}/{id}",
defaults: new { id = RouteParameter.Optional }
);
测试路线 -
http://localhost:60957/api/Another/Route/a
http://localhost:60957/api/Another/Route2/aaaaa