// 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 Microsoft.AspNetCore.Routing; using Microsoft.AspNetCore.Routing.Internal; using Microsoft.Extensions.DependencyInjection; namespace Microsoft.AspNetCore.Builder { /// /// Extension methods for adding the middleware to an . /// public static class RoutingBuilderExtensions { /// /// Adds a middleware to the specified with the specified . /// /// The to add the middleware to. /// The to use for routing requests. /// A reference to this instance after the operation has completed. public static IApplicationBuilder UseRouter(this IApplicationBuilder builder, IRouter router) { if (builder == null) { throw new ArgumentNullException(nameof(builder)); } if (router == null) { throw new ArgumentNullException(nameof(router)); } if (builder.ApplicationServices.GetService(typeof(RoutingMarkerService)) == null) { throw new InvalidOperationException(Resources.FormatUnableToFindServices( nameof(IServiceCollection), nameof(RoutingServiceCollectionExtensions.AddRouting), "ConfigureServices(...)")); } return builder.UseMiddleware(router); } /// /// Adds a middleware to the specified /// with the built from configured . /// /// The to add the middleware to. /// An to configure the provided . /// A reference to this instance after the operation has completed. public static IApplicationBuilder UseRouter(this IApplicationBuilder builder, Action action) { if (builder == null) { throw new ArgumentNullException(nameof(builder)); } if (action == null) { throw new ArgumentNullException(nameof(action)); } if (builder.ApplicationServices.GetService(typeof(RoutingMarkerService)) == null) { throw new InvalidOperationException(Resources.FormatUnableToFindServices( nameof(IServiceCollection), nameof(RoutingServiceCollectionExtensions.AddRouting), "ConfigureServices(...)")); } var routeBuilder = new RouteBuilder(builder); action(routeBuilder); return builder.UseRouter(routeBuilder.Build()); } } }