|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Net.Mime; |
| 7 | +using Microsoft.AspNetCore.Blazor.Server; |
| 8 | +using Microsoft.AspNetCore.StaticFiles; |
| 9 | +using Microsoft.Extensions.FileProviders; |
| 10 | + |
| 11 | +namespace Microsoft.AspNetCore.Builder |
| 12 | +{ |
| 13 | + /// <summary> |
| 14 | + /// Provides extension methods for hosting client-side Blazor applications in ASP.NET Core. |
| 15 | + /// </summary> |
| 16 | + public static class BlazorHostingApplicationBuilderExtensions |
| 17 | + { |
| 18 | + /// <summary> |
| 19 | + /// Adds a <see cref="StaticFileMiddleware"/> that will serve static files from the client-side Blazor application |
| 20 | + /// specified by <typeparamref name="TClientApp"/>. |
| 21 | + /// </summary> |
| 22 | + /// <typeparam name="TClientApp">A type in the client-side application.</typeparam> |
| 23 | + /// <param name="app">The <see cref="IApplicationBuilder"/>.</param> |
| 24 | + /// <returns>The <see cref="IApplicationBuilder"/>.</returns> |
| 25 | + public static IApplicationBuilder UseClientSideBlazorFiles<TClientApp>(this IApplicationBuilder app) |
| 26 | + { |
| 27 | + if (app == null) |
| 28 | + { |
| 29 | + throw new ArgumentNullException(nameof(app)); |
| 30 | + } |
| 31 | + |
| 32 | + UseClientSideBlazorFiles(app, typeof(TClientApp).Assembly.Location); |
| 33 | + return app; |
| 34 | + } |
| 35 | + |
| 36 | + /// <summary> |
| 37 | + /// Adds a <see cref="StaticFileMiddleware"/> that will serve static files from the client-side Blazor application |
| 38 | + /// specified by <paramref name="clientAssemblyFilePath"/>. |
| 39 | + /// </summary> |
| 40 | + /// <param name="clientAssemblyFilePath">The file path of the client-side Blazor application assembly.</param> |
| 41 | + /// <param name="app">The <see cref="IApplicationBuilder"/>.</param> |
| 42 | + /// <returns>The <see cref="IApplicationBuilder"/>.</returns> |
| 43 | + public static IApplicationBuilder UseClientSideBlazorFiles(this IApplicationBuilder app, string clientAssemblyFilePath) |
| 44 | + { |
| 45 | + if (clientAssemblyFilePath == null) |
| 46 | + { |
| 47 | + throw new ArgumentNullException(nameof(clientAssemblyFilePath)); |
| 48 | + } |
| 49 | + |
| 50 | + var fileProviders = new List<IFileProvider>(); |
| 51 | + |
| 52 | + // TODO: Make the .blazor.config file contents sane |
| 53 | + // Currently the items in it are bizarre and don't relate to their purpose, |
| 54 | + // hence all the path manipulation here. We shouldn't be hardcoding 'dist' here either. |
| 55 | + var config = BlazorConfig.Read(clientAssemblyFilePath); |
| 56 | + |
| 57 | + // First, match the request against files in the client app dist directory |
| 58 | + fileProviders.Add(new PhysicalFileProvider(config.DistPath)); |
| 59 | + |
| 60 | + // * Before publishing, we serve the wwwroot files directly from source |
| 61 | + // (and don't require them to be copied into dist). |
| 62 | + // In this case, WebRootPath will be nonempty if that directory exists. |
| 63 | + // * After publishing, the wwwroot files are already copied to 'dist' and |
| 64 | + // will be served by the above middleware, so we do nothing here. |
| 65 | + // In this case, WebRootPath will be empty (the publish process sets this). |
| 66 | + if (!string.IsNullOrEmpty(config.WebRootPath)) |
| 67 | + { |
| 68 | + fileProviders.Add(new PhysicalFileProvider(config.WebRootPath)); |
| 69 | + } |
| 70 | + |
| 71 | + // We can't modify an IFileContentTypeProvider, so we have to decorate. |
| 72 | + var contentTypeProvider = new FileExtensionContentTypeProvider(); |
| 73 | + AddMapping(contentTypeProvider, ".dll", MediaTypeNames.Application.Octet); |
| 74 | + if (config.EnableDebugging) |
| 75 | + { |
| 76 | + AddMapping(contentTypeProvider, ".pdb", MediaTypeNames.Application.Octet); |
| 77 | + } |
| 78 | + |
| 79 | + var options = new StaticFileOptions() |
| 80 | + { |
| 81 | + ContentTypeProvider = contentTypeProvider, |
| 82 | + FileProvider = new CompositeFileProvider(fileProviders), |
| 83 | + OnPrepareResponse = CacheHeaderSettings.SetCacheHeaders, |
| 84 | + }; |
| 85 | + |
| 86 | + app.UseStaticFiles(options); |
| 87 | + return app; |
| 88 | + |
| 89 | + static void AddMapping(FileExtensionContentTypeProvider provider, string name, string mimeType) |
| 90 | + { |
| 91 | + if (!provider.Mappings.ContainsKey(name)) |
| 92 | + { |
| 93 | + provider.Mappings.Add(name, mimeType); |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments