/// <summary> /// Maps the attribute-defined routes for the application. /// </summary> /// <param name="configuration">The server configuration.</param> /// <param name="constraintResolver">The <see cref="IInlineConstraintResolver"/> to use for resolving inline constraints.</param> public static void MapHttpAttributeRoutes(this HttpConfiguration configuration, IInlineConstraintResolver constraintResolver) { HttpRouteBuilder routeBuilder = new HttpRouteBuilder(constraintResolver); var attrRoute = new RouteCollectionRoute(); configuration.Routes.Add(AttributeRouteName, attrRoute); Action <HttpConfiguration> previousInitializer = configuration.Initializer; configuration.Initializer = config => { // Chain to the previous initializer hook. Do this before we access the config since // initialization may make last minute changes to the configuration. previousInitializer(config); // Add a single placeholder route that handles all of attribute routing. // Add an initialize hook that initializes these routes after the config has been initialized. Func <HttpSubRouteCollection> initializer = () => MapHttpAttributeRoutesInternal(configuration, routeBuilder); // This won't change config. It wants to pick up the finalized config. HttpSubRouteCollection subRoutes = attrRoute.EnsureInitialized(initializer); if (subRoutes != null) { AddGenerationHooksForSubRoutes(config.Routes, subRoutes, routeBuilder); } }; }
private static HttpRouteEntry CreateRouteEntry(string routeTemplate) { IHttpRoute route = new HttpRouteBuilder().BuildHttpRoute(routeTemplate, new HttpMethod[] { HttpMethod.Get }, actions: null); return(new HttpRouteEntry() { Route = route, RouteTemplate = routeTemplate }); }
public void Descriptions_RecognizesIgnoreApiForDirectRoutes_Controller() { var config = new HttpConfiguration(); var routeTemplate = "api/values"; var controllerDescriptor = new HttpControllerDescriptor(config, "IgnoreApiValues", typeof(IgnoreApiValuesController)); var actions = new ReflectedHttpActionDescriptor[] { new ReflectedHttpActionDescriptor(controllerDescriptor, typeof(IgnoreApiValuesController).GetMethod("Get")), new ReflectedHttpActionDescriptor(controllerDescriptor, typeof(IgnoreApiValuesController).GetMethod("Post")), }; config.Routes.Add("Route", HttpRouteBuilder.BuildDirectRoute(routeTemplate, 0, actions)); var descriptions = new ApiExplorer(config).ApiDescriptions; Assert.Empty(descriptions); }
public void Descriptions_RecognizesDirectRoutes() { var config = new HttpConfiguration(); var routeTemplate = "api/values"; var controllerDescriptor = new HttpControllerDescriptor(config, "ApiExplorerValues", typeof(ApiExplorerValuesController)); var action = new ReflectedHttpActionDescriptor(controllerDescriptor, typeof(ApiExplorerValuesController).GetMethod("Get")); var actions = new ReflectedHttpActionDescriptor[] { action }; config.Routes.Add("Route", HttpRouteBuilder.BuildDirectRoute(routeTemplate, 0, actions)); var descriptions = new ApiExplorer(config).ApiDescriptions; ApiDescription description = Assert.Single(descriptions); Assert.Equal(HttpMethod.Get, description.HttpMethod); Assert.Equal(routeTemplate, description.RelativePath); Assert.Equal(action, description.ActionDescriptor); }
public void Descriptions_RecognizesCompositeRoutes() { var config = new HttpConfiguration(); var routeTemplate = "api/values"; var controllerDescriptor = new HttpControllerDescriptor(config, "AttributeApiExplorerValues", typeof(AttributeApiExplorerValuesController)); var action = new ReflectedHttpActionDescriptor(controllerDescriptor, typeof(AttributeApiExplorerValuesController).GetMethod("Action")); var actions = new ReflectedHttpActionDescriptor[] { action }; var routeCollection = new HttpSubRouteCollection(); routeCollection.Add("testroute", HttpRouteBuilder.BuildDirectRoute(routeTemplate, 0, actions)); RouteCollectionRoute route = new RouteCollectionRoute(); route.EnsureInitialized(() => routeCollection); config.Routes.Add("Route", route); var descriptions = new ApiExplorer(config).ApiDescriptions; ApiDescription description = Assert.Single(descriptions); Assert.Equal(HttpMethod.Get, description.HttpMethod); Assert.Equal(routeTemplate, description.RelativePath); Assert.Equal(action, description.ActionDescriptor); }
private static HttpRouteEntry CreateRouteEntry(string routeTemplate) { IHttpRoute route = new HttpRouteBuilder().BuildHttpRoute(routeTemplate, actions: null); return new HttpRouteEntry() { Route = route, RouteTemplate = routeTemplate }; }
protected GenericHttpApiServiceClient(IAccessTokenService authService, ServerAddress serverAddress) { ApiRouteBuilder = new HttpRouteBuilder(serverAddress); AuthService = authService; }
private static HttpSubRouteCollection MapHttpAttributeRoutesInternal(this HttpConfiguration configuration, HttpRouteBuilder routeBuilder) { HttpSubRouteCollection subRoutes = new HttpSubRouteCollection(); if (configuration == null) { throw Error.ArgumentNull("configuration"); } if (routeBuilder == null) { throw Error.ArgumentNull("routeBuilder"); } List <HttpRouteEntry> attributeRoutes = new List <HttpRouteEntry>(); IHttpControllerSelector controllerSelector = configuration.Services.GetHttpControllerSelector(); IDictionary <string, HttpControllerDescriptor> controllerMap = controllerSelector.GetControllerMapping(); if (controllerMap != null) { foreach (HttpControllerDescriptor controllerDescriptor in controllerMap.Values) { IEnumerable <HttpRouteEntry> controllerRoutes = CreateRouteEntries(controllerDescriptor); foreach (HttpRouteEntry route in controllerRoutes) { route.Route = routeBuilder.BuildParsingRoute(route.Template, route.Order, route.Actions); } attributeRoutes.AddRange(controllerRoutes); } foreach (HttpRouteEntry attributeRoute in attributeRoutes) { IHttpRoute route = attributeRoute.Route; if (route != null) { subRoutes.Add(attributeRoute.Name, attributeRoute.Route); } } } return(subRoutes); }
// Add generation hooks for the Attribute-routing subroutes. // This lets us generate urls for routes supplied by attr-based routing. private static void AddGenerationHooksForSubRoutes(HttpRouteCollection destRoutes, HttpSubRouteCollection sourceRoutes, HttpRouteBuilder routeBuilder) { foreach (KeyValuePair <string, IHttpRoute> kv in sourceRoutes.NamedRoutes) { string name = kv.Key; IHttpRoute route = kv.Value; var stubRoute = routeBuilder.BuildGenerationRoute(route); destRoutes.Add(name, stubRoute); } }
/// <summary> /// Maps the attribute-defined routes for the application. /// </summary> /// <param name="configuration">The server configuration.</param> /// <param name="routeBuilder">The <see cref="HttpRouteBuilder"/> to use for generating attribute routes.</param> public static void MapHttpAttributeRoutes(this HttpConfiguration configuration, HttpRouteBuilder routeBuilder) { if (configuration == null) { throw Error.ArgumentNull("configuration"); } if (routeBuilder == null) { throw Error.ArgumentNull("routeBuilder"); } List <HttpRouteEntry> attributeRoutes = new List <HttpRouteEntry>(); IHttpControllerSelector controllerSelector = configuration.Services.GetHttpControllerSelector(); IDictionary <string, HttpControllerDescriptor> controllerMap = controllerSelector.GetControllerMapping(); if (controllerMap != null) { foreach (HttpControllerDescriptor controllerDescriptor in controllerMap.Values) { IEnumerable <HttpRouteEntry> controllerRoutes = CreateRouteEntries(controllerDescriptor); foreach (HttpRouteEntry route in controllerRoutes) { route.Route = routeBuilder.BuildHttpRoute(route.RouteTemplate, route.HttpMethods, route.Actions); } SetDefaultRouteNames(controllerRoutes, controllerDescriptor.ControllerName); attributeRoutes.AddRange(controllerRoutes); } attributeRoutes.Sort(); foreach (HttpRouteEntry attributeRoute in attributeRoutes) { IHttpRoute route = attributeRoute.Route; if (route != null) { configuration.Routes.Add(attributeRoute.Name, attributeRoute.Route); } } } }
/// <summary> /// Maps the attribute-defined routes for the application. /// </summary> /// <param name="configuration">The server configuration.</param> /// <param name="routeBuilder">The <see cref="HttpRouteBuilder"/> to use for generating attribute routes.</param> public static void MapHttpAttributeRoutes(this HttpConfiguration configuration, HttpRouteBuilder routeBuilder) { if (configuration == null) { throw Error.ArgumentNull("configuration"); } if (routeBuilder == null) { throw Error.ArgumentNull("routeBuilder"); } List <HttpRouteEntry> attributeRoutes = new List <HttpRouteEntry>(); IHttpControllerSelector controllerSelector = configuration.Services.GetHttpControllerSelector(); IDictionary <string, HttpControllerDescriptor> controllerMapping = controllerSelector.GetControllerMapping(); if (controllerMapping != null) { foreach (HttpControllerDescriptor controllerDescriptor in controllerMapping.Values) { Collection <RoutePrefixAttribute> routePrefixes = controllerDescriptor.GetCustomAttributes <RoutePrefixAttribute>(inherit: false); IHttpActionSelector actionSelector = controllerDescriptor.Configuration.Services.GetActionSelector(); ILookup <string, HttpActionDescriptor> actionMapping = actionSelector.GetActionMapping(controllerDescriptor); if (actionMapping != null) { foreach (IGrouping <string, HttpActionDescriptor> actionGrouping in actionMapping) { string controllerName = controllerDescriptor.ControllerName; attributeRoutes.AddRange(CreateAttributeRoutes(routeBuilder, controllerName, routePrefixes, actionGrouping)); } } } attributeRoutes.Sort(); foreach (HttpRouteEntry attributeRoute in attributeRoutes) { configuration.Routes.Add(attributeRoute.Name, attributeRoute.Route); } } }
private static List <HttpRouteEntry> CreateAttributeRoutes(HttpRouteBuilder routeBuilder, string controllerName, Collection <RoutePrefixAttribute> routePrefixes, IGrouping <string, HttpActionDescriptor> actionGrouping) { List <HttpRouteEntry> routes = new List <HttpRouteEntry>(); string actionName = actionGrouping.Key; foreach (HttpActionDescriptor actionDescriptor in actionGrouping) { IEnumerable <IHttpRouteInfoProvider> routeInfoProviders = actionDescriptor.GetCustomAttributes <IHttpRouteInfoProvider>(inherit: false); // DefaultIfEmpty below is required to add routes when there is a route prefix but no // route provider or when there is a route provider with a template but no route prefix foreach (RoutePrefixAttribute routePrefix in routePrefixes.DefaultIfEmpty()) { foreach (IHttpRouteInfoProvider routeProvider in routeInfoProviders.DefaultIfEmpty()) { string prefixTemplate = routePrefix == null ? null : routePrefix.Prefix; string providerTemplate = routeProvider == null ? null : routeProvider.RouteTemplate; if (prefixTemplate == null && providerTemplate == null) { continue; } ValidateTemplates(prefixTemplate, providerTemplate, actionDescriptor); string routeTemplate; if (String.IsNullOrEmpty(prefixTemplate)) { routeTemplate = providerTemplate ?? String.Empty; } else if (String.IsNullOrEmpty(providerTemplate)) { routeTemplate = prefixTemplate; } else { // template and prefix both not null - combine them routeTemplate = prefixTemplate + '/' + providerTemplate; } Collection <HttpMethod> httpMethods = actionDescriptor.SupportedHttpMethods; IHttpRoute route = routeBuilder.BuildHttpRoute(routeTemplate, httpMethods, controllerName, actionName); HttpRouteEntry entry = new HttpRouteEntry() { Route = route, RouteTemplate = routeTemplate }; if (routeProvider != null) { entry.Name = routeProvider.RouteName; entry.Order = routeProvider.RouteOrder; } if (routePrefix != null) { entry.PrefixOrder = routePrefix.Order; } routes.Add(entry); } } } SetDefaultRouteNames(routes, controllerName, actionName); return(routes); }