Skip to content

Change MapBlazorWebAssemblyApplication to UseBlazorFrameworkFiles #19198

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Components/WebAssembly/DevServer/src/Server/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment environment,

app.UseBlazorDebugging();

app.UseBlazorFrameworkFiles();
app.UseStaticFiles();

app.UseRouting();

app.UseEndpoints(endpoints =>
{
endpoints.MapBlazorWebAssemblyApplication();
endpoints.MapFallbackToFile("index.html");
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Net.Mime;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.StaticFiles;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;
using Microsoft.Net.Http.Headers;

namespace Microsoft.AspNetCore.Builder
{
/// <summary>
/// Extensions for mapping Blazor WebAssembly applications.
/// </summary>
public static class ComponentsWebAssemblyApplicationBuilderExtensions
{
/// <summary>
/// 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.
/// </summary>
/// <param name="builder">The <see cref="IApplicationBuilder"/>.</param>
/// <param name="pathPrefix">The <see cref="PathString"/> that indicates the prefix for the Blazor WebAssembly application.</param>
/// <returns>The <see cref="IApplicationBuilder"/></returns>
public static IApplicationBuilder MapBlazorFrameworkFiles(this IApplicationBuilder builder, PathString pathPrefix)
{
if (builder is null)
{
throw new ArgumentNullException(nameof(builder));
}

var webHostEnvironment = builder.ApplicationServices.GetRequiredService<IWebHostEnvironment>();

var options = CreateStaticFilesOptions(webHostEnvironment.WebRootFileProvider);

builder.MapWhen(ctx => ctx.Request.Path.StartsWithSegments(pathPrefix, out var rest) && rest.StartsWithSegments("/_framework") && !rest.StartsWithSegments("/_framework/blazor.server.js"),
subBuilder =>
{
subBuilder.Use(async (ctx, next) =>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of interest, would an alternative working implementation have been to configure the "on prepare response" callback for options and put the header controls in there, then just have subBuilder.UseStaticFiles(options) without the extra bit of middleware?

I'm not saying that's better. Just want to understand the options.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just did the most straightforward transformation from what we had before, using OnPrepareResponse would have also worked.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the difference you'd seen in behavior here is that OnPrepareResponse would handle the cases where the file was found - this will add a header for a 404.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think you are right, not a big deal since I plan to work on this for preview 3 and a 404 can’t be cached anyways?

{
// At this point we mapped something from the /_framework
ctx.Response.Headers.Append(HeaderNames.CacheControl, "no-cache");
// This will invoke the static files middleware plugged-in below.
await next();
});

subBuilder.UseStaticFiles(options);
});

return builder;
}

/// <summary>
/// Configures the application to serve Blazor WebAssembly framework files from the root path "/".
/// </summary>
/// <param name="applicationBuilder">The <see cref="IApplicationBuilder"/>.</param>
/// <param name="pathPrefix">The <see cref="PathString"/> that indicates the prefix for the Blazor WebAssembly application.</param>
/// <returns>The <see cref="IApplicationBuilder"/></returns>
public static IApplicationBuilder UseBlazorFrameworkFiles(this IApplicationBuilder applicationBuilder) =>
MapBlazorFrameworkFiles(applicationBuilder, default);

private static StaticFileOptions CreateStaticFilesOptions(IFileProvider webRootFileProvider)
{
var options = new StaticFileOptions();
options.FileProvider = webRootFileProvider;
var contentTypeProvider = new FileExtensionContentTypeProvider();
AddMapping(contentTypeProvider, ".dll", MediaTypeNames.Application.Octet);
// We unconditionally map pdbs as there will be no pdbs in the output folder for
// release builds unless BlazorEnableDebugging is explicitly set to true.
AddMapping(contentTypeProvider, ".pdb", MediaTypeNames.Application.Octet);

options.ContentTypeProvider = contentTypeProvider;

return options;
}

private static void AddMapping(FileExtensionContentTypeProvider provider, string name, string mimeType)
{
if (!provider.Mappings.ContainsKey(name))
{
provider.Mappings.Add(name, mimeType);
}
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, RequestL
app.UseBlazorDebugging();
}

app.UseBlazorFrameworkFiles();
app.UseStaticFiles();

app.UseRouting();

app.UseEndpoints(endpoints =>
{
endpoints.MapBlazorWebAssemblyApplication();
endpoints.MapFallbackToFile("index.html");
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ public void Configure(IApplicationBuilder app)
{
app.UseDeveloperExceptionPage();
app.UseFileServer(new FileServerOptions() { EnableDefaultFiles = true, });
app.UseBlazorFrameworkFiles();
app.UseStaticFiles();
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapBlazorWebAssemblyApplication();
endpoints.MapFallbackToFile("index.html");
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
app.UseBlazorDebugging();
}

app.UseBlazorFrameworkFiles();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthentication();
Expand All @@ -67,7 +70,6 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
endpoints.MapControllers();
endpoints.MapRazorPages();

endpoints.MapBlazorWebAssemblyApplication();
endpoints.MapFallbackToFile("index.html");
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
// Mount the server-side Blazor app on /subdir
app.Map("/subdir", app =>
{
app.UseBlazorFrameworkFiles();
app.UseStaticFiles();

app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapBlazorWebAssemblyApplication();
endpoints.MapControllers();
endpoints.MapRazorPages();
endpoints.MapBlazorHub();
Expand Down
3 changes: 1 addition & 2 deletions src/Components/test/testassets/TestServer/ClientStartup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,12 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
app.Map("/subdir", app =>
{
// Add it before to ensure it takes priority over files in wwwroot
app.UseBlazorFrameworkFiles();
app.UseStaticFiles();

app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapBlazorWebAssemblyApplication();

endpoints.MapRazorPages();
endpoints.MapControllers();
endpoints.MapFallbackToFile("index.html");
Expand Down
3 changes: 1 addition & 2 deletions src/Components/test/testassets/TestServer/CorsStartup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
// Mount the server-side Blazor app on /subdir
app.Map("/subdir", app =>
{
app.UseBlazorFrameworkFiles();
app.UseStaticFiles();

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

app.UseEndpoints(endpoints =>
{
endpoints.MapBlazorWebAssemblyApplication();

endpoints.MapControllers();
endpoints.MapFallbackToFile("index.html");
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
// Mount the server-side Blazor app on /subdir
app.Map("/subdir", app =>
{
app.UseBlazorFrameworkFiles();
app.UseStaticFiles();

app.UseRequestLocalization(options =>
Expand All @@ -54,8 +55,6 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapBlazorWebAssemblyApplication();

endpoints.MapControllers();
endpoints.MapBlazorHub();
endpoints.MapFallbackToPage("/_ServerHost");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,8 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
// The client-side files middleware needs to be here because the base href in hardcoded to /subdir/
app.Map("/subdir", app =>
{
app.UseRouting();

app.UseEndpoints(endpoints =>
{
endpoints.MapBlazorWebAssemblyApplication();
});
app.UseBlazorFrameworkFiles();
app.UseStaticFiles();
});

// The calls to `Map` allow us to test each of these overloads, while keeping them isolated.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
}

#endif
app.UseBlazorFrameworkFiles();
app.UseStaticFiles();

app.UseRouting();
Expand All @@ -124,8 +125,6 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
endpoints.MapRazorPages();
#endif
endpoints.MapControllers();

endpoints.MapBlazorWebAssemblyApplication();
endpoints.MapFallbackToFile("index.html");
});
}
Expand Down