// 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; using System.Text.Encodings.Web; using Microsoft.AspNetCore.Dispatcher; using Microsoft.AspNetCore.Routing; using Microsoft.AspNetCore.Routing.Dispatcher; using Microsoft.AspNetCore.Routing.Internal; using Microsoft.AspNetCore.Routing.Tree; using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.ObjectPool; namespace Microsoft.Extensions.DependencyInjection { /// /// Contains extension methods to . /// public static class RoutingServiceCollectionExtensions { /// /// Adds services required for routing requests. /// /// The to add the services to. /// The so that additional calls can be chained. public static IServiceCollection AddRouting(this IServiceCollection services) { if (services == null) { throw new ArgumentNullException(nameof(services)); } // Routing shares lots of infrastructure with the dispatcher. services.AddDispatcher(); services.TryAddSingleton(); services.TryAddTransient(); services.TryAddSingleton(UrlEncoder.Default); // The TreeRouteBuilder is a builder for creating routes, it should stay transient because it's // stateful. services.TryAddTransient(); services.TryAddSingleton(typeof(RoutingMarkerService)); return services; } /// /// Adds services required for routing requests. /// /// The to add the services to. /// The routing options to configure the middleware with. /// The so that additional calls can be chained. public static IServiceCollection AddRouting( this IServiceCollection services, Action configureOptions) { if (services == null) { throw new ArgumentNullException(nameof(services)); } if (configureOptions == null) { throw new ArgumentNullException(nameof(configureOptions)); } services.Configure(configureOptions); services.AddRouting(); return services; } } }