Skip to content

Commit e745d3a

Browse files
committed
Change MapBlazorWebAssemblyApplication to UseBlazorFrameworkFiles
1 parent e9ccd7f commit e745d3a

File tree

11 files changed

+34
-63
lines changed

11 files changed

+34
-63
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
}

src/Components/WebAssembly/Server/src/ComponentsWebAssemblyEndpointRouteBuilderExtensions.cs renamed to src/Components/WebAssembly/Server/src/ComponentsWebAssemblyApplicationBuilderExtensions.cs

Lines changed: 19 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using System.Net.Mime;
66
using Microsoft.AspNetCore.Hosting;
77
using Microsoft.AspNetCore.Http;
8-
using Microsoft.AspNetCore.Routing;
98
using Microsoft.AspNetCore.StaticFiles;
109
using Microsoft.Extensions.DependencyInjection;
1110
using Microsoft.Extensions.FileProviders;
@@ -16,74 +15,50 @@ namespace Microsoft.AspNetCore.Builder
1615
/// <summary>
1716
/// Extensions for mapping Blazor WebAssembly applications.
1817
/// </summary>
19-
public static class ComponentsWebAssemblyEndpointRouteBuilderExtensions
18+
public static class ComponentsWebAssemblyApplicationBuilderExtensions
2019
{
2120
/// <summary>
2221
/// Maps a Blazor webassembly application to the <paramref name="pathPrefix"/>.
2322
/// </summary>
24-
/// <param name="endpoints">The <see cref="IEndpointRouteBuilder"/>.</param>
23+
/// <param name="builder">The <see cref="IApplicationBuilder"/>.</param>
2524
/// <param name="pathPrefix">The <see cref="PathString"/> that indicates the prefix for the Blazor application.</param>
26-
/// <returns>The <see cref="IEndpointConventionBuilder"/></returns>
27-
public static IEndpointConventionBuilder MapBlazorWebAssemblyApplication(this IEndpointRouteBuilder endpoints, PathString pathPrefix)
25+
/// <returns>The <see cref="IApplicationBuilder"/></returns>
26+
public static IApplicationBuilder MapBlazorFrameworkFiles(this IApplicationBuilder builder, PathString pathPrefix)
2827
{
29-
if (endpoints is null)
28+
if (builder is null)
3029
{
31-
throw new ArgumentNullException(nameof(endpoints));
30+
throw new ArgumentNullException(nameof(builder));
3231
}
3332

34-
var webHostEnvironment = endpoints.ServiceProvider.GetRequiredService<IWebHostEnvironment>();
33+
var webHostEnvironment = builder.ApplicationServices.GetRequiredService<IWebHostEnvironment>();
3534

3635
var options = CreateStaticFilesOptions(webHostEnvironment.WebRootFileProvider);
37-
var appBuilder = endpoints.CreateApplicationBuilder();
3836

39-
appBuilder.Use(async (ctx, next) =>
37+
builder.MapWhen(ctx => ctx.Request.Path.StartsWithSegments(pathPrefix, out var rest) && rest.StartsWithSegments("/_framework") && !rest.StartsWithSegments("/_framework/blazor.server.js"),
38+
subBuilder =>
4039
{
41-
var endpoint = ctx.GetEndpoint();
42-
try
40+
subBuilder.Use(async (ctx, next) =>
4341
{
44-
// Set the endpoint to null so that static files doesn't discard the path.
45-
ctx.SetEndpoint(null);
46-
47-
if (ctx.Request.Path.StartsWithSegments(pathPrefix, out var rest) &&
48-
rest.StartsWithSegments("/_framework"))
49-
{
50-
// At this point we mapped something from the /_framework
51-
ctx.Response.Headers.Append(HeaderNames.CacheControl, "no-cache");
52-
}
53-
42+
// At this point we mapped something from the /_framework
43+
ctx.Response.Headers.Append(HeaderNames.CacheControl, "no-cache");
5444
// This will invoke the static files middleware plugged-in below.
5545
await next();
46+
});
5647

57-
}
58-
finally
59-
{
60-
ctx.SetEndpoint(endpoint);
61-
}
62-
});
63-
64-
appBuilder.UseStaticFiles(options);
65-
66-
var conventionBuilder = endpoints.Map(
67-
$"{pathPrefix}/{{*path:file}}",
68-
appBuilder.Build());
69-
70-
conventionBuilder.Add(builder =>
71-
{
72-
// Map this route with low priority so that it doesn't interfere with any other potential request.
73-
((RouteEndpointBuilder)builder).Order = int.MaxValue - 100;
48+
subBuilder.UseStaticFiles(options);
7449
});
7550

76-
return conventionBuilder;
51+
return builder;
7752
}
7853

7954
/// <summary>
8055
/// Maps a Blazor webassembly application to the root path of the application "/".
8156
/// </summary>
82-
/// <param name="endpoints">The <see cref="IEndpointRouteBuilder"/>.</param>
57+
/// <param name="applicationBuilder">The <see cref="IApplicationBuilder"/>.</param>
8358
/// <param name="pathPrefix">The <see cref="PathString"/> that indicates the prefix for the Blazor application.</param>
84-
/// <returns>The <see cref="IEndpointConventionBuilder"/></returns>
85-
public static IEndpointConventionBuilder MapBlazorWebAssemblyApplication(this IEndpointRouteBuilder endpoints) =>
86-
MapBlazorWebAssemblyApplication(endpoints, default);
59+
/// <returns>The <see cref="IApplicationBuilder"/></returns>
60+
public static IApplicationBuilder UseBlazorFrameworkFiles(this IApplicationBuilder applicationBuilder) =>
61+
MapBlazorFrameworkFiles(applicationBuilder, default);
8762

8863
private static StaticFileOptions CreateStaticFilesOptions(IFileProvider webRootFileProvider)
8964
{

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)