httpRoutes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/v{version}/{controller}/{id}", defaults: new { id = RouteParameter.Optional } );
public class HttpControllerSelector : DefaultHttpControllerSelector { private readonly HttpConfiguration configuration; public HttpControllerSelector(HttpConfiguration configuration) : base(configuration) { this.configuration = configuration; } public override HttpControllerDescriptor SelectController(HttpRequestMessage request) { } }
container.Register( Component.For<IHttpControllerSelector>().ImplementedBy<HttpControllerSelector>().DependsOn( Dependency.OnValue<HttpConfiguration>(GlobalConfiguration.Configuration)));
HttpControllerDescriptor(HttpConfiguration, String, Type)
var controllerName = GetControllerName(request);
public class HttpControllerSelector : DefaultHttpControllerSelector { private readonly HttpConfiguration configuration; private readonly Lazy<ConcurrentDictionary<string, Type>> controllerTypes; public HttpControllerSelector(HttpConfiguration configuration) : base(configuration) { this.configuration = configuration; controllerTypes = new Lazy<ConcurrentDictionary<string, Type>>(GetControllerTypes); } public override HttpControllerDescriptor SelectController(HttpRequestMessage request) { var controllerName = GetControllerName(request); } private static ConcurrentDictionary<string, Type> GetControllerTypes() { var assemblies = AppDomain.CurrentDomain.GetAssemblies(); var types = assemblies .SelectMany(a => a.GetTypes().Where(t => !t.IsAbstract && t.Name.EndsWith(ControllerSuffix, StringComparison.OrdinalIgnoreCase) && typeof (IHttpController).IsAssignableFrom(t))) .ToDictionary(t => t.FullName, t => t); return new ConcurrentDictionary<string, Type>(types); } }
object version; request.GetRouteData().Values.TryGetValue("version", out version);
var type = GetControllerType((string)version, controllerName);
private Type GetControllerType(string version, string controllerName) { var query = controllerTypes.Value.AsEnumerable(); return query.ByVersion(version) .ByControllerName(controllerName) .Select(x => x.Value) .Single(); }
public static IEnumerable<KeyValuePair<string, Type>> ByVersion(this IEnumerable<KeyValuePair<string, Type>> query, string version) { var versionNamespace = string.Format(CultureInfo.InvariantCulture, ".V{0}.", version); return query.Where(x => x.Key.IndexOf(versionNamespace, StringComparison.OrdinalIgnoreCase) != -1); } public static IEnumerable<KeyValuePair<string, Type>> ByControllerName(this IEnumerable<KeyValuePair<string, Type>> query, string controllerName) { var controllerNameToFind = string.Format(CultureInfo.InvariantCulture, ".{0}{1}", controllerName, DefaultHttpControllerSelector.ControllerSuffix); return query.Where(x => x.Key.EndsWith(controllerNameToFind, StringComparison.OrdinalIgnoreCase)); }
public class HttpControllerSelector : DefaultHttpControllerSelector { private readonly HttpConfiguration configuration; private readonly Lazy<ConcurrentDictionary<string, Type>> controllerTypes; public HttpControllerSelector(HttpConfiguration configuration) : base(configuration) { this.configuration = configuration; controllerTypes = new Lazy<ConcurrentDictionary<string, Type>>(GetControllerTypes); } public override HttpControllerDescriptor SelectController(HttpRequestMessage request) { object version; request.GetRouteData().Values.TryGetValue("version", out version); var controllerName = GetControllerName(request); var type = GetControllerType((string)version, controllerName); return new HttpControllerDescriptor(configuration, controllerName, type); } private static ConcurrentDictionary<string, Type> GetControllerTypes() { var assemblies = AppDomain.CurrentDomain.GetAssemblies(); var types = assemblies .SelectMany(a => a.GetTypes().Where(t => !t.IsAbstract && t.Name.EndsWith(ControllerSuffix, StringComparison.OrdinalIgnoreCase) && typeof (IHttpController).IsAssignableFrom(t))) .ToDictionary(t => t.FullName, t => t); return new ConcurrentDictionary<string, Type>(types); } private Type GetControllerType(string version, string controllerName) { var query = controllerTypes.Value.AsEnumerable(); return query.ByVersion(version) .ByControllerName(controllerName) .Select(x => x.Value) .Single(); } } public static class ControllerTypeSpecifications { public static IEnumerable<KeyValuePair<string, Type>> ByVersion(this IEnumerable<KeyValuePair<string, Type>> query, string version) { var versionNamespace = string.Format(CultureInfo.InvariantCulture, ".V{0}.", version); return query.Where(x => x.Key.IndexOf(versionNamespace, StringComparison.OrdinalIgnoreCase) != -1); } public static IEnumerable<KeyValuePair<string, Type>> ByControllerName(this IEnumerable<KeyValuePair<string, Type>> query, string controllerName) { var controllerNameToFind = string.Format(CultureInfo.InvariantCulture, ".{0}{1}", controllerName, DefaultHttpControllerSelector.ControllerSuffix); return query.Where(x => x.Key.EndsWith(controllerNameToFind, StringComparison.OrdinalIgnoreCase)); } }
Controllers.Api.V1.UserController Controllers.Api.V2.UserController
Source: https://habr.com/ru/post/165149/
All Articles