Skip to content

Commit 9ded8e1

Browse files
authored
Change MapBlazorWebAssemblyApplication to UseBlazorFrameworkFiles (#19198)
1 parent c5f5325 commit 9ded8e1

File tree

12 files changed

+101
-130
lines changed

12 files changed

+101
-130
lines changed

src/Components/WebAssembly/DevServer/src/Server/Startup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,13 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment environment,
4646

4747
app.UseBlazorDebugging();
4848

49+
app.UseBlazorFrameworkFiles();
4950
app.UseStaticFiles();
5051

5152
app.UseRouting();
5253

5354
app.UseEndpoints(endpoints =>
5455
{
55-
endpoints.MapBlazorWebAssemblyApplication();
5656
endpoints.MapFallbackToFile("index.html");
5757
});
5858
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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.Net.Mime;
6+
using Microsoft.AspNetCore.Hosting;
7+
using Microsoft.AspNetCore.Http;
8+
using Microsoft.AspNetCore.StaticFiles;
9+
using Microsoft.Extensions.DependencyInjection;
10+
using Microsoft.Extensions.FileProviders;
11+
using Microsoft.Net.Http.Headers;
12+
13+
namespace Microsoft.AspNetCore.Builder
14+
{
15+
/// <summary>
16+
/// Extensions for mapping Blazor WebAssembly applications.
17+
/// </summary>
18+
public static class ComponentsWebAssemblyApplicationBuilderExtensions
19+
{
20+
/// <summary>
21+
/// Configures the application to serve Blazor WebAssembly framework files from the path <paramref name="pathPrefix"/>. This path must correspond to a referenced Blazor WebAssembly application project.
22+
/// </summary>
23+
/// <param name="builder">The <see cref="IApplicationBuilder"/>.</param>
24+
/// <param name="pathPrefix">The <see cref="PathString"/> that indicates the prefix for the Blazor WebAssembly application.</param>
25+
/// <returns>The <see cref="IApplicationBuilder"/></returns>
26+
public static IApplicationBuilder MapBlazorFrameworkFiles(this IApplicationBuilder builder, PathString pathPrefix)
27+
{
28+
if (builder is null)
29+
{
30+
throw new ArgumentNullException(nameof(builder));
31+
}
32+
33+
var webHostEnvironment = builder.ApplicationServices.GetRequiredService<IWebHostEnvironment>();
34+
35+
var options = CreateStaticFilesOptions(webHostEnvironment.WebRootFileProvider);
36+
37+
builder.MapWhen(ctx => ctx.Request.Path.StartsWithSegments(pathPrefix, out var rest) && rest.StartsWithSegments("/_framework") && !rest.StartsWithSegments("/_framework/blazor.server.js"),
38+
subBuilder =>
39+
{
40+
subBuilder.Use(async (ctx, next) =>
41+
{
42+
// At this point we mapped something from the /_framework
43+
ctx.Response.Headers.Append(HeaderNames.CacheControl, "no-cache");
44+
// This will invoke the static files middleware plugged-in below.
45+
await next();
46+
});
47+
48+
subBuilder.UseStaticFiles(options);
49+
});
50+
51+
return builder;
52+
}
53+
54+
/// <summary>
55+
/// Configures the application to serve Blazor WebAssembly framework files from the root path "/".
56+
/// </summary>
57+
/// <param name="applicationBuilder">The <see cref="IApplicationBuilder"/>.</param>
58+
/// <param name="pathPrefix">The <see cref="PathString"/> that indicates the prefix for the Blazor WebAssembly application.</param>
59+
/// <returns>The <see cref="IApplicationBuilder"/></returns>
60+
public static IApplicationBuilder UseBlazorFrameworkFiles(this IApplicationBuilder applicationBuilder) =>
61+
MapBlazorFrameworkFiles(applicationBuilder, default);
62+
63+
private static StaticFileOptions CreateStaticFilesOptions(IFileProvider webRootFileProvider)
64+
{
65+
var options = new StaticFileOptions();
66+
options.FileProvider = webRootFileProvider;
67+
var contentTypeProvider = new FileExtensionContentTypeProvider();
68+
AddMapping(contentTypeProvider, ".dll", MediaTypeNames.Application.Octet);
69+
// We unconditionally map pdbs as there will be no pdbs in the output folder for
70+
// release builds unless BlazorEnableDebugging is explicitly set to true.
71+
AddMapping(contentTypeProvider, ".pdb", MediaTypeNames.Application.Octet);
72+
73+
options.ContentTypeProvider = contentTypeProvider;
74+
75+
return options;
76+
}
77+
78+
private static void AddMapping(FileExtensionContentTypeProvider provider, string name, string mimeType)
79+
{
80+
if (!provider.Mappings.ContainsKey(name))
81+
{
82+
provider.Mappings.Add(name, mimeType);
83+
}
84+
}
85+
}
86+
}

src/Components/WebAssembly/Server/src/ComponentsWebAssemblyEndpointRouteBuilderExtensions.cs

Lines changed: 0 additions & 111 deletions
This file was deleted.

src/Components/WebAssembly/testassets/HostedInAspNet.Server/Startup.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,13 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, RequestL
3333
app.UseBlazorDebugging();
3434
}
3535

36+
app.UseBlazorFrameworkFiles();
37+
app.UseStaticFiles();
38+
3639
app.UseRouting();
3740

3841
app.UseEndpoints(endpoints =>
3942
{
40-
endpoints.MapBlazorWebAssemblyApplication();
4143
endpoints.MapFallbackToFile("index.html");
4244
});
4345
}

src/Components/WebAssembly/testassets/MonoSanity/Startup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ public void Configure(IApplicationBuilder app)
1616
{
1717
app.UseDeveloperExceptionPage();
1818
app.UseFileServer(new FileServerOptions() { EnableDefaultFiles = true, });
19+
app.UseBlazorFrameworkFiles();
1920
app.UseStaticFiles();
2021
app.UseRouting();
2122
app.UseEndpoints(endpoints =>
2223
{
23-
endpoints.MapBlazorWebAssemblyApplication();
2424
endpoints.MapFallbackToFile("index.html");
2525
});
2626
}

src/Components/WebAssembly/testassets/Wasm.Authentication.Server/Startup.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
5656
app.UseBlazorDebugging();
5757
}
5858

59+
app.UseBlazorFrameworkFiles();
60+
app.UseStaticFiles();
61+
5962
app.UseRouting();
6063

6164
app.UseAuthentication();
@@ -67,7 +70,6 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
6770
endpoints.MapControllers();
6871
endpoints.MapRazorPages();
6972

70-
endpoints.MapBlazorWebAssemblyApplication();
7173
endpoints.MapFallbackToFile("index.html");
7274
});
7375
}

src/Components/test/testassets/TestServer/AuthenticationStartup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,12 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
4949
// Mount the server-side Blazor app on /subdir
5050
app.Map("/subdir", app =>
5151
{
52+
app.UseBlazorFrameworkFiles();
5253
app.UseStaticFiles();
5354

5455
app.UseRouting();
5556
app.UseEndpoints(endpoints =>
5657
{
57-
endpoints.MapBlazorWebAssemblyApplication();
5858
endpoints.MapControllers();
5959
endpoints.MapRazorPages();
6060
endpoints.MapBlazorHub();

src/Components/test/testassets/TestServer/ClientStartup.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,12 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
3737
app.Map("/subdir", app =>
3838
{
3939
// Add it before to ensure it takes priority over files in wwwroot
40+
app.UseBlazorFrameworkFiles();
4041
app.UseStaticFiles();
4142

4243
app.UseRouting();
4344
app.UseEndpoints(endpoints =>
4445
{
45-
endpoints.MapBlazorWebAssemblyApplication();
46-
4746
endpoints.MapRazorPages();
4847
endpoints.MapControllers();
4948
endpoints.MapFallbackToFile("index.html");

src/Components/test/testassets/TestServer/CorsStartup.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
4545
// Mount the server-side Blazor app on /subdir
4646
app.Map("/subdir", app =>
4747
{
48+
app.UseBlazorFrameworkFiles();
4849
app.UseStaticFiles();
4950

5051
app.UseRouting();
@@ -53,8 +54,6 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
5354

5455
app.UseEndpoints(endpoints =>
5556
{
56-
endpoints.MapBlazorWebAssemblyApplication();
57-
5857
endpoints.MapControllers();
5958
endpoints.MapFallbackToFile("index.html");
6059
});

src/Components/test/testassets/TestServer/InternationalizationStartup.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
3636
// Mount the server-side Blazor app on /subdir
3737
app.Map("/subdir", app =>
3838
{
39+
app.UseBlazorFrameworkFiles();
3940
app.UseStaticFiles();
4041

4142
app.UseRequestLocalization(options =>
@@ -54,8 +55,6 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
5455
app.UseRouting();
5556
app.UseEndpoints(endpoints =>
5657
{
57-
endpoints.MapBlazorWebAssemblyApplication();
58-
5958
endpoints.MapControllers();
6059
endpoints.MapBlazorHub();
6160
endpoints.MapFallbackToPage("/_ServerHost");

src/Components/test/testassets/TestServer/StartupWithMapFallbackToClientSideBlazor.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,8 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
3333
// The client-side files middleware needs to be here because the base href in hardcoded to /subdir/
3434
app.Map("/subdir", app =>
3535
{
36-
app.UseRouting();
37-
38-
app.UseEndpoints(endpoints =>
39-
{
40-
endpoints.MapBlazorWebAssemblyApplication();
41-
});
36+
app.UseBlazorFrameworkFiles();
37+
app.UseStaticFiles();
4238
});
4339

4440
// The calls to `Map` allow us to test each of these overloads, while keeping them isolated.

src/ProjectTemplates/ComponentsWebAssembly.ProjectTemplates/content/ComponentsWebAssembly-CSharp/Server/Startup.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
104104
}
105105

106106
#endif
107+
app.UseBlazorFrameworkFiles();
107108
app.UseStaticFiles();
108109

109110
app.UseRouting();
@@ -124,8 +125,6 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
124125
endpoints.MapRazorPages();
125126
#endif
126127
endpoints.MapControllers();
127-
128-
endpoints.MapBlazorWebAssemblyApplication();
129128
endpoints.MapFallbackToFile("index.html");
130129
});
131130
}

0 commit comments

Comments
 (0)