Change MapBlazorWebAssemblyApplication to UseBlazorFrameworkFiles (#19198)
This commit is contained in:
parent
c5f532511c
commit
9ded8e17d4
|
|
@ -46,13 +46,13 @@ namespace Microsoft.AspNetCore.Components.WebAssembly.DevServer.Server
|
|||
|
||||
app.UseBlazorDebugging();
|
||||
|
||||
app.UseBlazorFrameworkFiles();
|
||||
app.UseStaticFiles();
|
||||
|
||||
app.UseRouting();
|
||||
|
||||
app.UseEndpoints(endpoints =>
|
||||
{
|
||||
endpoints.MapBlazorWebAssemblyApplication();
|
||||
endpoints.MapFallbackToFile("index.html");
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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) =>
|
||||
{
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,111 +0,0 @@
|
|||
// 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.Routing;
|
||||
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 ComponentsWebAssemblyEndpointRouteBuilderExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Maps a Blazor webassembly application to the <paramref name="pathPrefix"/>.
|
||||
/// </summary>
|
||||
/// <param name="endpoints">The <see cref="IEndpointRouteBuilder"/>.</param>
|
||||
/// <param name="pathPrefix">The <see cref="PathString"/> that indicates the prefix for the Blazor application.</param>
|
||||
/// <returns>The <see cref="IEndpointConventionBuilder"/></returns>
|
||||
public static IEndpointConventionBuilder MapBlazorWebAssemblyApplication(this IEndpointRouteBuilder endpoints, PathString pathPrefix)
|
||||
{
|
||||
if (endpoints is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(endpoints));
|
||||
}
|
||||
|
||||
var webHostEnvironment = endpoints.ServiceProvider.GetRequiredService<IWebHostEnvironment>();
|
||||
|
||||
var options = CreateStaticFilesOptions(webHostEnvironment.WebRootFileProvider);
|
||||
var appBuilder = endpoints.CreateApplicationBuilder();
|
||||
|
||||
appBuilder.Use(async (ctx, next) =>
|
||||
{
|
||||
var endpoint = ctx.GetEndpoint();
|
||||
try
|
||||
{
|
||||
// Set the endpoint to null so that static files doesn't discard the path.
|
||||
ctx.SetEndpoint(null);
|
||||
|
||||
if (ctx.Request.Path.StartsWithSegments(pathPrefix, out var rest) &&
|
||||
rest.StartsWithSegments("/_framework"))
|
||||
{
|
||||
// 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();
|
||||
|
||||
}
|
||||
finally
|
||||
{
|
||||
ctx.SetEndpoint(endpoint);
|
||||
}
|
||||
});
|
||||
|
||||
appBuilder.UseStaticFiles(options);
|
||||
|
||||
var conventionBuilder = endpoints.Map(
|
||||
$"{pathPrefix}/{{*path:file}}",
|
||||
appBuilder.Build());
|
||||
|
||||
conventionBuilder.Add(builder =>
|
||||
{
|
||||
// Map this route with low priority so that it doesn't interfere with any other potential request.
|
||||
((RouteEndpointBuilder)builder).Order = int.MaxValue - 100;
|
||||
});
|
||||
|
||||
return conventionBuilder;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Maps a Blazor webassembly application to the root path of the application "/".
|
||||
/// </summary>
|
||||
/// <param name="endpoints">The <see cref="IEndpointRouteBuilder"/>.</param>
|
||||
/// <param name="pathPrefix">The <see cref="PathString"/> that indicates the prefix for the Blazor application.</param>
|
||||
/// <returns>The <see cref="IEndpointConventionBuilder"/></returns>
|
||||
public static IEndpointConventionBuilder MapBlazorWebAssemblyApplication(this IEndpointRouteBuilder endpoints) =>
|
||||
MapBlazorWebAssemblyApplication(endpoints, 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -33,11 +33,13 @@ namespace HostedInAspNet.Server
|
|||
app.UseBlazorDebugging();
|
||||
}
|
||||
|
||||
app.UseBlazorFrameworkFiles();
|
||||
app.UseStaticFiles();
|
||||
|
||||
app.UseRouting();
|
||||
|
||||
app.UseEndpoints(endpoints =>
|
||||
{
|
||||
endpoints.MapBlazorWebAssemblyApplication();
|
||||
endpoints.MapFallbackToFile("index.html");
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,11 +16,11 @@ namespace MonoSanity
|
|||
{
|
||||
app.UseDeveloperExceptionPage();
|
||||
app.UseFileServer(new FileServerOptions() { EnableDefaultFiles = true, });
|
||||
app.UseBlazorFrameworkFiles();
|
||||
app.UseStaticFiles();
|
||||
app.UseRouting();
|
||||
app.UseEndpoints(endpoints =>
|
||||
{
|
||||
endpoints.MapBlazorWebAssemblyApplication();
|
||||
endpoints.MapFallbackToFile("index.html");
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -56,6 +56,9 @@ namespace Wasm.Authentication.Server
|
|||
app.UseBlazorDebugging();
|
||||
}
|
||||
|
||||
app.UseBlazorFrameworkFiles();
|
||||
app.UseStaticFiles();
|
||||
|
||||
app.UseRouting();
|
||||
|
||||
app.UseAuthentication();
|
||||
|
|
@ -67,7 +70,6 @@ namespace Wasm.Authentication.Server
|
|||
endpoints.MapControllers();
|
||||
endpoints.MapRazorPages();
|
||||
|
||||
endpoints.MapBlazorWebAssemblyApplication();
|
||||
endpoints.MapFallbackToFile("index.html");
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -49,12 +49,12 @@ namespace TestServer
|
|||
// 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();
|
||||
|
|
|
|||
|
|
@ -37,13 +37,12 @@ namespace TestServer
|
|||
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");
|
||||
|
|
|
|||
|
|
@ -45,6 +45,7 @@ namespace TestServer
|
|||
// Mount the server-side Blazor app on /subdir
|
||||
app.Map("/subdir", app =>
|
||||
{
|
||||
app.UseBlazorFrameworkFiles();
|
||||
app.UseStaticFiles();
|
||||
|
||||
app.UseRouting();
|
||||
|
|
@ -53,8 +54,6 @@ namespace TestServer
|
|||
|
||||
app.UseEndpoints(endpoints =>
|
||||
{
|
||||
endpoints.MapBlazorWebAssemblyApplication();
|
||||
|
||||
endpoints.MapControllers();
|
||||
endpoints.MapFallbackToFile("index.html");
|
||||
});
|
||||
|
|
|
|||
|
|
@ -36,6 +36,7 @@ namespace TestServer
|
|||
// Mount the server-side Blazor app on /subdir
|
||||
app.Map("/subdir", app =>
|
||||
{
|
||||
app.UseBlazorFrameworkFiles();
|
||||
app.UseStaticFiles();
|
||||
|
||||
app.UseRequestLocalization(options =>
|
||||
|
|
@ -54,8 +55,6 @@ namespace TestServer
|
|||
app.UseRouting();
|
||||
app.UseEndpoints(endpoints =>
|
||||
{
|
||||
endpoints.MapBlazorWebAssemblyApplication();
|
||||
|
||||
endpoints.MapControllers();
|
||||
endpoints.MapBlazorHub();
|
||||
endpoints.MapFallbackToPage("/_ServerHost");
|
||||
|
|
|
|||
|
|
@ -33,12 +33,8 @@ namespace TestServer
|
|||
// 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.
|
||||
|
|
|
|||
|
|
@ -104,6 +104,7 @@ namespace ComponentsWebAssembly_CSharp.Server
|
|||
}
|
||||
|
||||
#endif
|
||||
app.UseBlazorFrameworkFiles();
|
||||
app.UseStaticFiles();
|
||||
|
||||
app.UseRouting();
|
||||
|
|
@ -124,8 +125,6 @@ namespace ComponentsWebAssembly_CSharp.Server
|
|||
endpoints.MapRazorPages();
|
||||
#endif
|
||||
endpoints.MapControllers();
|
||||
|
||||
endpoints.MapBlazorWebAssemblyApplication();
|
||||
endpoints.MapFallbackToFile("index.html");
|
||||
});
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue