// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System.Collections.Generic; using System.Collections.ObjectModel; using System.Diagnostics.Contracts; using System.Web.Http.Controllers; using System.Web.Http.Properties; namespace System.Web.Http.Routing { /// /// A default implementation of . /// public class DefaultDirectRouteProvider : IDirectRouteProvider { /// /// Gets direct routes for the given controller descriptor and action descriptors based on /// attributes. /// /// The controller descriptor. /// The action descriptors for all actions. /// The constraint resolver. /// A set of route entries. /// /// The implementation returns route entries for the given controller and actions. /// /// Any actions that have associated instances will produce route /// entries that route direct to those actions. /// /// Any actions that do not have an associated instances will be /// associated with the controller. If the controller has any associated /// instances, then route entries will be created for the controller and associated actions. /// public virtual IReadOnlyList GetDirectRoutes( HttpControllerDescriptor controllerDescriptor, IReadOnlyList actionDescriptors, IInlineConstraintResolver constraintResolver) { List entries = new List(); List actionsWithoutRoutes = new List(); foreach (HttpActionDescriptor action in actionDescriptors) { IReadOnlyList factories = GetActionRouteFactories(action); if (factories != null && factories.Count > 0) { IReadOnlyCollection actionEntries = GetActionDirectRoutes(action, factories, constraintResolver); if (actionEntries != null) { entries.AddRange(actionEntries); } } else { // IF there are no routes on the specific action, attach it to the controller routes (if any). actionsWithoutRoutes.Add(action); } } if (actionsWithoutRoutes.Count > 0) { IReadOnlyList controllerFactories = GetControllerRouteFactories(controllerDescriptor); if (controllerFactories != null && controllerFactories.Count > 0) { IReadOnlyCollection controllerEntries = GetControllerDirectRoutes( controllerDescriptor, actionsWithoutRoutes, controllerFactories, constraintResolver); if (controllerEntries != null) { entries.AddRange(controllerEntries); } } } return entries; } /// /// Gets route factories for the given controller descriptor. /// /// The controller descriptor. /// A set of route factories. /// /// The implementation returns instances based on attributes on the controller. /// protected virtual IReadOnlyList GetControllerRouteFactories(HttpControllerDescriptor controllerDescriptor) { Collection newFactories = controllerDescriptor.GetCustomAttributes(inherit: false); Collection oldProviders = controllerDescriptor.GetCustomAttributes(inherit: false); List combined = new List(); combined.AddRange(newFactories); foreach (IHttpRouteInfoProvider oldProvider in oldProviders) { if (oldProvider is IDirectRouteFactory) { continue; } combined.Add(new RouteInfoDirectRouteFactory(oldProvider)); } return combined; } /// /// Gets a set of route factories for the given action descriptor. /// /// The action descriptor. /// A set of route factories. /// /// The implementation returns instances based on attributes on the action. Returns /// null if the action was defined on a base class of this controller. /// protected virtual IReadOnlyList GetActionRouteFactories(HttpActionDescriptor actionDescriptor) { // Ignore the Route attributes from inherited actions. ReflectedHttpActionDescriptor reflectedActionDescriptor = actionDescriptor as ReflectedHttpActionDescriptor; if (reflectedActionDescriptor != null && reflectedActionDescriptor.MethodInfo != null && reflectedActionDescriptor.MethodInfo.DeclaringType != actionDescriptor.ControllerDescriptor.ControllerType) { return null; } Collection newFactories = actionDescriptor.GetCustomAttributes(inherit: false); Collection oldProviders = actionDescriptor.GetCustomAttributes(inherit: false); List combined = new List(); combined.AddRange(newFactories); foreach (IHttpRouteInfoProvider oldProvider in oldProviders) { if (oldProvider is IDirectRouteFactory) { continue; } combined.Add(new RouteInfoDirectRouteFactory(oldProvider)); } return combined; } /// /// Creates instances based on the provided factories, controller and actions. The route /// entries provided direct routing to the provided controller and can reach the set of provided actions. /// /// The controller descriptor. /// The action descriptors. /// The direct route factories. /// The constraint resolver. /// A set of route entries. protected virtual IReadOnlyList GetControllerDirectRoutes( HttpControllerDescriptor controllerDescriptor, IReadOnlyList actionDescriptors, IReadOnlyList factories, IInlineConstraintResolver constraintResolver) { return CreateRouteEntries( GetRoutePrefix(controllerDescriptor), factories, actionDescriptors, constraintResolver, targetIsAction: false); } /// /// Creates instances based on the provided factories and action. The route entries /// provide direct routing to the provided action. /// /// The action descriptor. /// The direct route factories. /// The constraint resolver. /// A set of route entries. protected virtual IReadOnlyList GetActionDirectRoutes( HttpActionDescriptor actionDescriptor, IReadOnlyList factories, IInlineConstraintResolver constraintResolver) { return CreateRouteEntries( GetRoutePrefix(actionDescriptor.ControllerDescriptor), factories, new HttpActionDescriptor[] { actionDescriptor }, constraintResolver, targetIsAction: true); } /// /// Gets the route prefix from the provided controller. /// /// The controller descriptor. /// The route prefix or null. protected virtual string GetRoutePrefix(HttpControllerDescriptor controllerDescriptor) { Collection attributes = controllerDescriptor.GetCustomAttributes(inherit: false); if (attributes == null) { return null; } if (attributes.Count > 1) { string errorMessage = Error.Format(SRResources.RoutePrefix_CannotSupportMultiRoutePrefix, controllerDescriptor.ControllerType.FullName); throw new InvalidOperationException(errorMessage); } if (attributes.Count == 1) { IRoutePrefix attribute = attributes[0]; if (attribute != null) { string prefix = attribute.Prefix; if (prefix == null) { string errorMessage = Error.Format( SRResources.RoutePrefix_PrefixCannotBeNull, controllerDescriptor.ControllerType.FullName); throw new InvalidOperationException(errorMessage); } if (prefix.EndsWith("/", StringComparison.Ordinal)) { throw Error.InvalidOperation(SRResources.AttributeRoutes_InvalidPrefix, prefix, controllerDescriptor.ControllerName); } return prefix; } } return null; } private static IReadOnlyList CreateRouteEntries( string prefix, IReadOnlyCollection factories, IReadOnlyCollection actions, IInlineConstraintResolver constraintResolver, bool targetIsAction) { List entries = new List(); foreach (IDirectRouteFactory factory in factories) { RouteEntry entry = CreateRouteEntry(prefix, factory, actions, constraintResolver, targetIsAction); entries.Add(entry); } return entries; } private static RouteEntry CreateRouteEntry( string prefix, IDirectRouteFactory factory, IReadOnlyCollection actions, IInlineConstraintResolver constraintResolver, bool targetIsAction) { Contract.Assert(factory != null); DirectRouteFactoryContext context = new DirectRouteFactoryContext(prefix, actions, constraintResolver, targetIsAction); RouteEntry entry = factory.CreateRoute(context); if (entry == null) { throw Error.InvalidOperation(SRResources.TypeMethodMustNotReturnNull, typeof(IDirectRouteFactory).Name, "CreateRoute"); } DirectRouteBuilder.ValidateRouteEntry(entry); return entry; } } }