Enable SPA fallback routing on server
This commit is contained in:
parent
58af3e1638
commit
0b5294a8f5
|
|
@ -1,13 +1,12 @@
|
||||||
// Copyright (c) .NET Foundation. All rights reserved.
|
// 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.
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
using Microsoft.AspNetCore.StaticFiles;
|
using Microsoft.AspNetCore.Http;
|
||||||
using Microsoft.AspNetCore.Blazor.Server;
|
using Microsoft.AspNetCore.Blazor.Server;
|
||||||
|
using Microsoft.AspNetCore.StaticFiles;
|
||||||
using Microsoft.Extensions.FileProviders;
|
using Microsoft.Extensions.FileProviders;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Net.Mime;
|
using System.Net.Mime;
|
||||||
using System;
|
|
||||||
|
|
||||||
namespace Microsoft.AspNetCore.Builder
|
namespace Microsoft.AspNetCore.Builder
|
||||||
{
|
{
|
||||||
|
|
@ -43,19 +42,16 @@ namespace Microsoft.AspNetCore.Builder
|
||||||
var config = BlazorConfig.Read(options.ClientAssemblyPath);
|
var config = BlazorConfig.Read(options.ClientAssemblyPath);
|
||||||
var clientAppBinDir = Path.GetDirectoryName(config.SourceOutputAssemblyPath);
|
var clientAppBinDir = Path.GetDirectoryName(config.SourceOutputAssemblyPath);
|
||||||
var clientAppDistDir = Path.Combine(clientAppBinDir, "dist");
|
var clientAppDistDir = Path.Combine(clientAppBinDir, "dist");
|
||||||
var distFileProvider = new PhysicalFileProvider(clientAppDistDir);
|
var distDirStaticFiles = new StaticFileOptions
|
||||||
|
|
||||||
applicationBuilder.UseDefaultFiles(new DefaultFilesOptions
|
|
||||||
{
|
{
|
||||||
FileProvider = distFileProvider
|
FileProvider = new PhysicalFileProvider(clientAppDistDir),
|
||||||
});
|
|
||||||
|
|
||||||
applicationBuilder.UseStaticFiles(new StaticFileOptions
|
|
||||||
{
|
|
||||||
FileProvider = distFileProvider,
|
|
||||||
ContentTypeProvider = CreateContentTypeProvider(),
|
ContentTypeProvider = CreateContentTypeProvider(),
|
||||||
});
|
};
|
||||||
|
|
||||||
|
// First, match the request against files in the client app dist directory
|
||||||
|
applicationBuilder.UseStaticFiles(distDirStaticFiles);
|
||||||
|
|
||||||
|
// Next, match the request against static files in wwwroot
|
||||||
if (!string.IsNullOrEmpty(config.WebRootPath))
|
if (!string.IsNullOrEmpty(config.WebRootPath))
|
||||||
{
|
{
|
||||||
// In development, we serve the wwwroot files directly from source
|
// In development, we serve the wwwroot files directly from source
|
||||||
|
|
@ -67,8 +63,21 @@ namespace Microsoft.AspNetCore.Builder
|
||||||
FileProvider = new PhysicalFileProvider(config.WebRootPath)
|
FileProvider = new PhysicalFileProvider(config.WebRootPath)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Finally, use SPA fallback routing (serve default page for anything else,
|
||||||
|
// excluding /_framework/*)
|
||||||
|
applicationBuilder.MapWhen(IsNotFrameworkDir, childAppBuilder =>
|
||||||
|
{
|
||||||
|
childAppBuilder.UseSpa(spa =>
|
||||||
|
{
|
||||||
|
spa.Options.DefaultPageStaticFileOptions = distDirStaticFiles;
|
||||||
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static bool IsNotFrameworkDir(HttpContext context)
|
||||||
|
=> !context.Request.Path.StartsWithSegments("/_framework");
|
||||||
|
|
||||||
private static IContentTypeProvider CreateContentTypeProvider()
|
private static IContentTypeProvider CreateContentTypeProvider()
|
||||||
{
|
{
|
||||||
var result = new FileExtensionContentTypeProvider();
|
var result = new FileExtensionContentTypeProvider();
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.0.1" />
|
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.0.1" />
|
||||||
|
<PackageReference Include="Microsoft.AspNetCore.SpaServices.Extensions" Version="2.0.0-rc2-final" />
|
||||||
<PackageReference Include="Microsoft.Extensions.FileProviders.Physical" Version="2.0.0" />
|
<PackageReference Include="Microsoft.Extensions.FileProviders.Physical" Version="2.0.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue