Ejemplo n.º 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)
                });
            }
        }
        /// <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"/> with configured Blazor.</returns>
        public static IApplicationBuilder UseLocalBlazor(
            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    = (IHostingEnvironment)app.ApplicationServices.GetService(typeof(IHostingEnvironment));
            var config = BlazorConfig.Read(options.ClientAssemblyPath);

            // 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   = 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 = SetCacheHeaders
                });
            }
            // Accept debugger connections
            if (config.EnableDebugging)
            {
                // Disable MonoDebugProxy, since it require me to maintain copy of Blazor code
                // app.UseMonoDebugProxy();
            }
            // 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 = SetCacheHeaders
                };
                childAppBuilder.UseSpa(spa =>
                {
                    spa.Options.DefaultPageStaticFileOptions = indexHtmlStaticFileOptions;
                });
            });
            return(app);
        }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 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 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;
                });
            });
        }
        /// <summary>
        /// Adds a <see cref="StaticFileMiddleware"/> that will serve static files from the client-side Blazor application
        /// specified by <paramref name="clientAssemblyFilePath"/>.
        /// </summary>
        /// <param name="clientAssemblyFilePath">The file path of the client-side Blazor application assembly.</param>
        /// <param name="app">The <see cref="IApplicationBuilder"/>.</param>
        /// <returns>The <see cref="IApplicationBuilder"/>.</returns>
        public static IApplicationBuilder UseClientSideBlazorFiles(this IApplicationBuilder app, string clientAssemblyFilePath)
        {
            if (clientAssemblyFilePath == null)
            {
                throw new ArgumentNullException(nameof(clientAssemblyFilePath));
            }

            var fileProviders = new List <IFileProvider>();

            // 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 config = BlazorConfig.Read(clientAssemblyFilePath);

            // First, match the request against files in the client app dist directory
            fileProviders.Add(new PhysicalFileProvider(config.DistPath));

            // * 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))
            {
                fileProviders.Add(new PhysicalFileProvider(config.WebRootPath));
            }

            // We can't modify an IFileContentTypeProvider, so we have to decorate.
            var contentTypeProvider = new FileExtensionContentTypeProvider();

            AddMapping(contentTypeProvider, ".dll", MediaTypeNames.Application.Octet);
            if (config.EnableDebugging)
            {
                AddMapping(contentTypeProvider, ".pdb", MediaTypeNames.Application.Octet);
            }

            var options = new StaticFileOptions()
            {
                ContentTypeProvider = contentTypeProvider,
                FileProvider        = new CompositeFileProvider(fileProviders),
                OnPrepareResponse   = CacheHeaderSettings.SetCacheHeaders,
            };

            app.UseStaticFiles(options);
            return(app);
Ejemplo n.º 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)
        {
            // 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;
                });
            });
        }
        /// <summary>
        /// Adds a low-priority endpoint that will serve the the file specified by <paramref name="filePath"/> from the client-side
        /// Blazor application specified by <paramref name="clientAssemblyFilePath"/>.
        /// </summary>
        /// <param name="clientAssemblyFilePath">The file path of the client-side Blazor application assembly.</param>
        /// <param name="endpoints">The <see cref="IEndpointRouteBuilder"/>.</param>
        /// <param name="pattern">The route pattern to match.</param>
        /// <param name="filePath">
        /// The relative path to the entry point of the client-side application. The path is relative to the
        /// <see cref="IWebHostEnvironment.WebRootPath"/>, commonly <c>wwwroot</c>.
        /// </param>
        /// <returns>The <see cref="IApplicationBuilder"/>.</returns>
        /// <remarks>
        /// <para>
        /// This method is intended to handle cases where URL path of the request does not contain a filename, and no other
        /// endpoint has matched. This is convenient for routing requests for dynamic content to the client-side blazor
        /// application, while also allowing requests for non-existent files to result in an HTTP 404.
        /// </para>
        /// </remarks>
        public static IEndpointConventionBuilder MapFallbackToClientSideBlazor(this IEndpointRouteBuilder endpoints, string clientAssemblyFilePath, string pattern, string filePath)
        {
            if (endpoints == null)
            {
                throw new ArgumentNullException(nameof(endpoints));
            }

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

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

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

            var config = BlazorConfig.Read(clientAssemblyFilePath);

            // We want to serve "index.html" from whichever directory contains it in this priority order:
            // 1. Client app "dist" directory
            // 2. Client app "wwwroot" directory
            // 3. Server app "wwwroot" directory
            var directory = endpoints.ServiceProvider.GetRequiredService <IWebHostEnvironment>().WebRootPath;
            var indexHtml = config.FindIndexHtmlFile();

            if (indexHtml != null)
            {
                directory = Path.GetDirectoryName(indexHtml);
            }

            var options = new StaticFileOptions()
            {
                FileProvider      = new PhysicalFileProvider(directory),
                OnPrepareResponse = CacheHeaderSettings.SetCacheHeaders,
            };

            return(endpoints.MapFallbackToFile(pattern, filePath, options));
        }
Ejemplo n.º 9
0
        // TODO: Change this combination of APIs to make it possible to supply either
        // an assembly name (resolved to current bin dir) or full assembly path
        internal static void UseBlazorInternal(
            this IApplicationBuilder applicationBuilder,
            string clientAssemblyPath)
        {
            var config           = BlazorConfig.Read(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(),
            });
        }
Ejemplo n.º 10
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;
                });
            });
        }