-
Notifications
You must be signed in to change notification settings - Fork 10.4k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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"), | ||
javiercn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
subBuilder => | ||
{ | ||
subBuilder.Use(async (ctx, next) => | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 I'm not saying that's better. Just want to understand the options. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Uh oh!
There was an error while loading. Please reload this page.