Update SpaDefaultPageMiddleware for endpoints

This change makes the SpaDefaultPageMiddleware noop when an endpoint has
been chosen. This is a problem today because this usually runs after
routing, but its always terminal.

So even if endpoint routing selected something, this would still serve
the default file. We're adding more things that fill this niche, but it
seems like a good idea to fix the existing stuff.
This commit is contained in:
Ryan Nowak 2019-02-28 12:15:42 -08:00
parent 089f64c528
commit 8a46e8cd93
2 changed files with 15 additions and 2 deletions

View File

@ -3,6 +3,7 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http.Endpoints;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
@ -24,6 +25,12 @@ namespace Microsoft.AspNetCore.SpaServices
// Rewrite all requests to the default page
app.Use((context, next) =>
{
// If we have an Endpoint, then this is a deferred match - just noop.
if (context.GetEndpoint() != null)
{
return next();
}
context.Request.Path = options.DefaultPage;
return next();
});
@ -39,6 +46,12 @@ namespace Microsoft.AspNetCore.SpaServices
// present on disk), the SPA is definitely not going to work.
app.Use((context, next) =>
{
// If we have an Endpoint, then this is a deferred match - just noop.
if (context.GetEndpoint() != null)
{
return next();
}
var message = "The SPA default page middleware could not return the default page " +
$"'{options.DefaultPage}' because it was not found, and no other middleware " +
"handled the request.\n";

View File

@ -1,7 +1,8 @@
// 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.
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http.Endpoints;
using Microsoft.AspNetCore.SpaServices.StaticFiles;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Options;
@ -110,7 +111,6 @@ namespace Microsoft.Extensions.DependencyInjection
}
}
app.UseStaticFiles(staticFileOptions);
}