コード例 #1
0
        /// <summary>
        /// Configures the middleware pipeline to work with Blazor.
        /// </summary>
        /// <param name="applicationBuilder"></param>
        /// <param name="options"></param>
        public static void UseBlazor(
            this IApplicationBuilder applicationBuilder,
            BlazorOptions options)
        {
            var config           = BlazorConfig.Read(options.ClientAssemblyPath);
            var clientAppBinDir  = Path.GetDirectoryName(config.SourceOutputAssemblyPath);
            var clientAppDistDir = Path.Combine(clientAppBinDir, "dist");
            var distFileProvider = new PhysicalFileProvider(clientAppDistDir);

            applicationBuilder.UseDefaultFiles(new DefaultFilesOptions
            {
                FileProvider = distFileProvider
            });

            applicationBuilder.UseStaticFiles(new StaticFileOptions
            {
                FileProvider        = distFileProvider,
                ContentTypeProvider = CreateContentTypeProvider(),
            });

            if (!string.IsNullOrEmpty(config.WebRootPath))
            {
                // In development, we serve the wwwroot files directly from source
                // (and don't require them to be copied into dist).
                // TODO: When publishing is implemented, have config.WebRootPath set
                // to null so that it only serves files that were copied to dist
                applicationBuilder.UseStaticFiles(new StaticFileOptions
                {
                    FileProvider = new PhysicalFileProvider(config.WebRootPath)
                });
            }
        }
コード例 #2
0
        /// <summary>
        /// Configures the middleware pipeline to work with Blazor.
        /// </summary>
        /// <param name="applicationBuilder"></param>
        /// <param name="options"></param>
        public static void UseBlazor(
            this IApplicationBuilder applicationBuilder,
            BlazorOptions options)
        {
            // TODO: Make the .blazor.config file contents sane
            // Currently the items in it are bizarre and don't relate to their purpose,
            // hence all the path manipulation here. We shouldn't be hardcoding 'dist' here either.
            var env                = (IHostingEnvironment)applicationBuilder.ApplicationServices.GetService(typeof(IHostingEnvironment));
            var config             = BlazorConfig.Read(options.ClientAssemblyPath);
            var distDirStaticFiles = new StaticFileOptions
            {
                FileProvider        = new PhysicalFileProvider(config.DistPath),
                ContentTypeProvider = CreateContentTypeProvider(),
                OnPrepareResponse   = SetCacheHeaders
            };

            if (env.IsDevelopment() && config.EnableAutoRebuilding)
            {
                if (env.ApplicationName.Equals(DevServerApplicationName, StringComparison.OrdinalIgnoreCase))
                {
                    applicationBuilder.UseDevServerAutoRebuild(config);
                }
                else
                {
                    applicationBuilder.UseHostedAutoRebuild(config, env.ContentRootPath);
                }
            }

            // First, match the request against files in the client app dist directory
            applicationBuilder.UseStaticFiles(distDirStaticFiles);

            // Next, match the request against static files in wwwroot
            if (!string.IsNullOrEmpty(config.WebRootPath))
            {
                // In development, we serve the wwwroot files directly from source
                // (and don't require them to be copied into dist).
                // TODO: When publishing is implemented, have config.WebRootPath set
                // to null so that it only serves files that were copied to dist
                applicationBuilder.UseStaticFiles(new StaticFileOptions
                {
                    FileProvider      = new PhysicalFileProvider(config.WebRootPath),
                    OnPrepareResponse = SetCacheHeaders
                });
            }

            // Finally, use SPA fallback routing (serve default page for anything else,
            // excluding /_framework/*)
            applicationBuilder.MapWhen(IsNotFrameworkDir, childAppBuilder =>
            {
                childAppBuilder.UseSpa(spa =>
                {
                    spa.Options.DefaultPageStaticFileOptions = distDirStaticFiles;
                });
            });
        }
        /// <summary>
        /// Configures the middleware pipeline to work with Blazor.
        /// </summary>
        /// <param name="aApplicationBuilder">The <see cref="IApplicationBuilder"/>.</param>
        /// <param name="aBlazorOptions">Options to configure the middleware.</param>
        /// <returns>The <see cref="IApplicationBuilder"/>.</returns>
        public static IApplicationBuilder UseBlazorDualMode(
            this IApplicationBuilder aApplicationBuilder,
            BlazorOptions aBlazorOptions)
        {
            aApplicationBuilder.UseStaticFiles();
            // TODO: Make the .blazor.config file contents sane
            // Currently the items in it are bizarre and don't relate to their purpose,
            // hence all the path manipulation here. We shouldn't be hardcoding 'dist' here either.
            var env    = (IHostingEnvironment)aApplicationBuilder.ApplicationServices.GetService(typeof(IHostingEnvironment));
            var config = BlazorConfig.Read(aBlazorOptions.ClientAssemblyPath);

            // The rebuild stuff is private and I dont' want to pull in all the source.
            // I frankly never get the auto stuff to work anyway
            //if (env.IsDevelopment() && config.EnableAutoRebuilding)
            //{
            //  if (env.ApplicationName.Equals(DevServerApplicationName, StringComparison.OrdinalIgnoreCase))
            //  {
            //    app.UseDevServerAutoRebuild(config);
            //  }
            //  else
            //  {
            //    //app.UseHostedAutoRebuild(config, env.ContentRootPath);
            //  }
            //}

            // First, match the request against files in the client app dist directory
            aApplicationBuilder.UseStaticFiles(new StaticFileOptions
            {
                FileProvider        = new PhysicalFileProvider(config.DistPath),
                ContentTypeProvider = CreateContentTypeProvider(config.EnableDebugging),
                OnPrepareResponse   = SetCacheHeaders
            });

            // * Before publishing, we serve the wwwroot files directly from source
            //   (and don't require them to be copied into dist).
            //   In this case, WebRootPath will be nonempty if that directory exists.
            // * After publishing, the wwwroot files are already copied to 'dist' and
            //   will be served by the above middleware, so we do nothing here.
            //   In this case, WebRootPath will be empty (the publish process sets this).
            if (!string.IsNullOrEmpty(config.WebRootPath))
            {
                aApplicationBuilder.UseStaticFiles(new StaticFileOptions
                {
                    FileProvider      = new PhysicalFileProvider(config.WebRootPath),
                    OnPrepareResponse = SetCacheHeaders
                });
            }

            return(aApplicationBuilder);
        }
コード例 #4
0
        /// <summary>
        /// Configures the middleware pipeline to work with Blazor.
        /// </summary>
        /// <param name="applicationBuilder"></param>
        /// <param name="options"></param>
        public static void UseBlazor(
            this IApplicationBuilder applicationBuilder,
            BlazorOptions options)
        {
            // TODO: Make the .blazor.config file contents sane
            // Currently the items in it are bizarre and don't relate to their purpose,
            // hence all the path manipulation here. We shouldn't be hardcoding 'dist' here either.
            var env                = (IHostingEnvironment)applicationBuilder.ApplicationServices.GetService(typeof(IHostingEnvironment));
            var config             = BlazorConfig.Read(options.ClientAssemblyPath);
            var distDirStaticFiles = new StaticFileOptions
            {
                FileProvider        = new PhysicalFileProvider(config.DistPath),
                ContentTypeProvider = CreateContentTypeProvider(),
            };

            // First, match the request against files in the client app dist directory
            applicationBuilder.UseStaticFiles(distDirStaticFiles);

            // Next, match the request against static files in wwwroot
            if (!string.IsNullOrEmpty(config.WebRootPath))
            {
                // In development, we serve the wwwroot files directly from source
                // (and don't require them to be copied into dist).
                // TODO: When publishing is implemented, have config.WebRootPath set
                // to null so that it only serves files that were copied to dist
                applicationBuilder.UseStaticFiles(new StaticFileOptions
                {
                    FileProvider = new PhysicalFileProvider(config.WebRootPath)
                });
            }

            // Definitely don't open a listener for live reloading in production, even if the
            // client app was compiled with live reloading enabled
            if (env.IsDevelopment())
            {
                // Whether or not live reloading is actually enabled depends on the client config
                // For release builds, it won't be (by default)
                applicationBuilder.UseBlazorLiveReloading(config);
            }

            // Finally, use SPA fallback routing (serve default page for anything else,
            // excluding /_framework/*)
            applicationBuilder.MapWhen(IsNotFrameworkDir, childAppBuilder =>
            {
                childAppBuilder.UseSpa(spa =>
                {
                    spa.Options.DefaultPageStaticFileOptions = distDirStaticFiles;
                });
            });
        }
コード例 #5
0
        /// <summary>
        /// Configures the middleware pipeline to work with Blazor.
        /// </summary>
        /// <param name="applicationBuilder"></param>
        /// <param name="options"></param>
        public static void UseBlazor(
            this IApplicationBuilder applicationBuilder,
            BlazorOptions options)
        {
            // TODO: Make the .blazor.config file contents sane
            // Currently the items in it are bizarre and don't relate to their purpose,
            // hence all the path manipulation here. We shouldn't be hardcoding 'dist' here either.
            var env              = (IHostingEnvironment)applicationBuilder.ApplicationServices.GetService(typeof(IHostingEnvironment));
            var config           = BlazorConfig.Read(options.ClientAssemblyPath);
            var clientAppBinDir  = Path.GetDirectoryName(config.SourceOutputAssemblyPath);
            var clientAppDistDir = Path.Combine(
                env.ContentRootPath,
                Path.Combine(clientAppBinDir, "dist"));
            var distDirStaticFiles = new StaticFileOptions
            {
                FileProvider        = new PhysicalFileProvider(clientAppDistDir),
                ContentTypeProvider = CreateContentTypeProvider(),
            };

            // First, match the request against files in the client app dist directory
            applicationBuilder.UseStaticFiles(distDirStaticFiles);

            // Next, match the request against static files in wwwroot
            if (!string.IsNullOrEmpty(config.WebRootPath))
            {
                // In development, we serve the wwwroot files directly from source
                // (and don't require them to be copied into dist).
                // TODO: When publishing is implemented, have config.WebRootPath set
                // to null so that it only serves files that were copied to dist
                applicationBuilder.UseStaticFiles(new StaticFileOptions
                {
                    FileProvider = new PhysicalFileProvider(config.WebRootPath)
                });
            }

            // Finally, use SPA fallback routing (serve default page for anything else,
            // excluding /_framework/*)
            applicationBuilder.MapWhen(IsNotFrameworkDir, childAppBuilder =>
            {
                childAppBuilder.UseSpa(spa =>
                {
                    spa.Options.DefaultPageStaticFileOptions = distDirStaticFiles;
                });
            });
        }
コード例 #6
0
        /// <summary>
        /// Registers middleware for Server-Side Blazor.
        /// </summary>
        /// <param name="builder">The <see cref="IApplicationBuilder"/>.</param>
        /// <param name="options">A <see cref="BlazorOptions"/> instance used to configure the Blazor file provider.</param>
        /// <param name="startupAction">A delegate used to configure the renderer.</param>
        /// <returns>The <see cref="IApplicationBuilder"/>.</returns>
        public static IApplicationBuilder UseServerSideBlazor(
            this IApplicationBuilder builder,
            BlazorOptions options,
            Action <IBlazorApplicationBuilder> startupAction)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            if (startupAction == null)
            {
                throw new ArgumentNullException(nameof(startupAction));
            }

            UseServerSideBlazorCore(builder, startupAction);

            builder.UseBlazor(options);

            return(builder);
        }
コード例 #7
0
        /// <summary>
        /// Configures the middleware pipeline to work with Blazor.
        /// </summary>
        /// <param name="applicationBuilder"></param>
        /// <param name="options"></param>
        public static void UseBlazor(
            this IApplicationBuilder applicationBuilder,
            BlazorOptions options)
        {
            var config             = BlazorConfig.Read(options.ClientAssemblyPath);
            var clientAppBinDir    = Path.GetDirectoryName(config.SourceOutputAssemblyPath);
            var clientAppDistDir   = Path.Combine(clientAppBinDir, "dist");
            var distDirStaticFiles = new StaticFileOptions
            {
                FileProvider        = new PhysicalFileProvider(clientAppDistDir),
                ContentTypeProvider = CreateContentTypeProvider(),
            };

            // First, match the request against files in the client app dist directory
            applicationBuilder.UseStaticFiles(distDirStaticFiles);

            // Next, match the request against static files in wwwroot
            if (!string.IsNullOrEmpty(config.WebRootPath))
            {
                // In development, we serve the wwwroot files directly from source
                // (and don't require them to be copied into dist).
                // TODO: When publishing is implemented, have config.WebRootPath set
                // to null so that it only serves files that were copied to dist
                applicationBuilder.UseStaticFiles(new StaticFileOptions
                {
                    FileProvider = new PhysicalFileProvider(config.WebRootPath)
                });
            }

            // Finally, use SPA fallback routing (serve default page for anything else,
            // excluding /_framework/*)
            applicationBuilder.MapWhen(IsNotFrameworkDir, childAppBuilder =>
            {
                childAppBuilder.UseSpa(spa =>
                {
                    spa.Options.DefaultPageStaticFileOptions = distDirStaticFiles;
                });
            });
        }
コード例 #8
0
        /// <summary>
        /// Registers Server-Side Blazor in the pipeline.
        /// </summary>
        /// <param name="builder">The <see cref="IApplicationBuilder"/>.</param>
        /// <param name="options">A <see cref="BlazorOptions"/> instance used to configure the Blazor file provider.</param>
        /// <returns>The <see cref="IApplicationBuilder"/>.</returns>
        public static IApplicationBuilder UseServerSideBlazor(
            this IApplicationBuilder builder,
            BlazorOptions options)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            // WARNING: Don't add extra setup logic here. It's important for
            // UseServerSideBlazor just to be shorthand for UseSignalR+UseBlazor,
            // so that people who want to call those two manually instead can
            // also do so. That's needed for people using Azure SignalR.

            // TODO: Also allow configuring the endpoint path.
            return(UseSignalRWithBlazorHub(builder, BlazorHub.DefaultPath)
                   .UseBlazor(options));
        }
コード例 #9
0
        /// <summary>
        /// Configures the middleware pipeline to work with Blazor.
        /// </summary>
        /// <param name="app">The <see cref="IApplicationBuilder"/>.</param>
        /// <param name="options">Options to configure the middleware.</param>
        /// <returns>The <see cref="IApplicationBuilder"/>.</returns>
        public static IApplicationBuilder UseBlazor(
            this IApplicationBuilder app,
            BlazorOptions options)
        {
            // TODO: Make the .blazor.config file contents sane
            // Currently the items in it are bizarre and don't relate to their purpose,
            // hence all the path manipulation here. We shouldn't be hardcoding 'dist' here either.
            var env    = (IWebHostEnvironment)app.ApplicationServices.GetService(typeof(IWebHostEnvironment));
            var config = BlazorConfig.Read(options.ClientAssemblyPath);

            if (env.IsDevelopment() && config.EnableAutoRebuilding)
            {
                if (env.ApplicationName.Equals(DevServerApplicationName, StringComparison.OrdinalIgnoreCase))
                {
                    app.UseDevServerAutoRebuild(config);
                }
                else
                {
                    app.UseHostedAutoRebuild(config, env.ContentRootPath);
                }
            }

            // First, match the request against files in the client app dist directory
            app.UseStaticFiles(new StaticFileOptions
            {
                FileProvider        = new PhysicalFileProvider(config.DistPath),
                ContentTypeProvider = CreateContentTypeProvider(config.EnableDebugging),
                OnPrepareResponse   = CacheHeaderSettings.SetCacheHeaders,
            });

            // * Before publishing, we serve the wwwroot files directly from source
            //   (and don't require them to be copied into dist).
            //   In this case, WebRootPath will be nonempty if that directory exists.
            // * After publishing, the wwwroot files are already copied to 'dist' and
            //   will be served by the above middleware, so we do nothing here.
            //   In this case, WebRootPath will be empty (the publish process sets this).
            if (!string.IsNullOrEmpty(config.WebRootPath))
            {
                app.UseStaticFiles(new StaticFileOptions
                {
                    FileProvider      = new PhysicalFileProvider(config.WebRootPath),
                    OnPrepareResponse = CacheHeaderSettings.SetCacheHeaders,
                });
            }

            // Finally, use SPA fallback routing (serve default page for anything else,
            // excluding /_framework/*)
            app.MapWhen(IsNotFrameworkDir, childAppBuilder =>
            {
                var indexHtmlPath = FindIndexHtmlFile(config);
                var indexHtmlStaticFileOptions = string.IsNullOrEmpty(indexHtmlPath)
                    ? null : new StaticFileOptions
                {
                    FileProvider      = new PhysicalFileProvider(Path.GetDirectoryName(indexHtmlPath)),
                    OnPrepareResponse = CacheHeaderSettings.SetCacheHeaders,
                };

                childAppBuilder.UseSpa(spa =>
                {
                    spa.Options.DefaultPageStaticFileOptions = indexHtmlStaticFileOptions;
                });
            });

            return(app);
        }