/// <summary>
        /// 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.
        /// </summary>
        /// <param name="app">The <see cref="IApplicationBuilder"/>.</param>
        /// <param name="urlPrefix">
        /// The URL path, relative to your application's <c>PathBase</c>, from which the
        /// SPA files are served.
        ///
        /// For example, if your SPA files are located in <c>wwwroot/dist</c>, then
        /// the value should usually be <c>"dist"</c>, because that is the URL prefix
        /// from which browsers can request those files.
        /// </param>
        /// <param name="sourcePath">
        /// Optional. If specified, configures the path (relative to the application working
        /// directory) of the directory that holds the SPA source files during development.
        /// The directory need not exist once the application is published.
        /// </param>
        /// <param name="defaultPage">
        /// Optional. If specified, configures the path (relative to <paramref name="urlPrefix"/>)
        /// of the default page that hosts your SPA user interface.
        /// If not specified, the default value is <c>"index.html"</c>.
        /// </param>
        /// <param name="configure">
        /// Optional. If specified, this callback will be invoked so that additional middleware
        /// can be registered within the context of this SPA.
        /// </param>
        public static void UseSpa(
            this IApplicationBuilder app,
            string urlPrefix,
            string sourcePath              = null,
            string defaultPage             = null,
            Action <ISpaOptions> configure = null)
        {
            var spaOptions = new DefaultSpaOptions(sourcePath, urlPrefix);

            spaOptions.RegisterSoleInstanceInPipeline(app);

            // Invoke 'configure' to give the developer a chance to insert extra
            // middleware before the 'default page' pipeline entries
            configure?.Invoke(spaOptions);

            SpaDefaultPageMiddleware.Attach(app, spaOptions);
        }
Exemple #2
0
        /// <summary>
        /// Handles requests by passing them through to an instance of the Angular CLI server.
        /// This means you can always serve up-to-date CLI-built resources without having
        /// to run the Angular CLI server manually.
        ///
        /// This feature should only be used in development. For production deployments, be
        /// sure not to enable the Angular CLI server.
        /// </summary>
        /// <param name="app">The <see cref="IApplicationBuilder"/>.</param>
        /// <param name="npmScript">The name of the script in your package.json file that launches the Angular CLI process.</param>
        public static void UseAngularCliServer(
            this IApplicationBuilder app,
            string npmScript)
        {
            var spaOptions = DefaultSpaOptions.FindInPipeline(app);

            if (spaOptions == null)
            {
                throw new InvalidOperationException($"{nameof(UseAngularCliServer)} should be called inside the 'configure' callback of a call to {nameof(SpaApplicationBuilderExtensions.UseSpa)}.");
            }

            if (string.IsNullOrEmpty(spaOptions.SourcePath))
            {
                throw new InvalidOperationException($"To use {nameof(UseAngularCliServer)}, you must supply a non-empty value for the {nameof(ISpaOptions.SourcePath)} property of {nameof(ISpaOptions)} when calling {nameof(SpaApplicationBuilderExtensions.UseSpa)}.");
            }

            AngularCliMiddleware.Attach(app, spaOptions.SourcePath, npmScript);
        }
        /// <inheritdoc />
        public Task Build(IApplicationBuilder app)
        {
            var spaOptions = DefaultSpaOptions.FindInPipeline(app);

            if (spaOptions == null)
            {
                throw new InvalidOperationException($"{nameof(AngularCliBuilder)} can only be used in an application configured with {nameof(SpaApplicationBuilderExtensions.UseSpa)}().");
            }

            if (string.IsNullOrEmpty(spaOptions.SourcePath))
            {
                throw new InvalidOperationException($"To use {nameof(AngularCliBuilder)}, you must supply a non-empty value for the {nameof(ISpaOptions.SourcePath)} property of {nameof(ISpaOptions)} when calling {nameof(SpaApplicationBuilderExtensions.UseSpa)}.");
            }

            return(StartAngularCliBuilderAsync(
                       _npmScriptName,
                       spaOptions.SourcePath,
                       AngularCliMiddleware.GetOrCreateLogger(app)));
        }