// 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 Microsoft.AspNetCore.SpaServices; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; using System; namespace Microsoft.AspNetCore.Builder { /// /// Provides extension methods used for configuring an application to /// host a client-side Single Page Application (SPA). /// public static class SpaApplicationBuilderExtensions { /// /// Handles all requests from this point in the middleware chain by returning /// the default page for the Single Page Application (SPA). /// /// This middleware should be placed late in the chain, so that other middleware /// for serving static files, MVC actions, etc., takes precedence. /// /// The . /// /// This callback will be invoked so that additional middleware can be registered within /// the context of this SPA. /// public static void UseSpa(this IApplicationBuilder app, Action configuration) { if (configuration == null) { throw new ArgumentNullException(nameof(configuration)); } // Use the options configured in DI (or blank if none was configured). We have to clone it // otherwise if you have multiple UseSpa calls, their configurations would interfere with one another. var optionsProvider = app.ApplicationServices.GetService>(); var options = new SpaOptions(optionsProvider.Value); var spaBuilder = new DefaultSpaBuilder(app, options); configuration.Invoke(spaBuilder); SpaDefaultPageMiddleware.Attach(spaBuilder); } } }