From 8a46e8cd93801a04e00d50a33962263b7d0e82ba Mon Sep 17 00:00:00 2001 From: Ryan Nowak Date: Thu, 28 Feb 2019 12:15:42 -0800 Subject: [PATCH] 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. --- .../src/SpaDefaultPageMiddleware.cs | 13 +++++++++++++ .../src/StaticFiles/SpaStaticFilesExtensions.cs | 4 ++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/Middleware/SpaServices.Extensions/src/SpaDefaultPageMiddleware.cs b/src/Middleware/SpaServices.Extensions/src/SpaDefaultPageMiddleware.cs index e07c7e28b7..35c31fe79e 100644 --- a/src/Middleware/SpaServices.Extensions/src/SpaDefaultPageMiddleware.cs +++ b/src/Middleware/SpaServices.Extensions/src/SpaDefaultPageMiddleware.cs @@ -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"; diff --git a/src/Middleware/SpaServices.Extensions/src/StaticFiles/SpaStaticFilesExtensions.cs b/src/Middleware/SpaServices.Extensions/src/StaticFiles/SpaStaticFilesExtensions.cs index 7036aca32d..36a5c3236d 100644 --- a/src/Middleware/SpaServices.Extensions/src/StaticFiles/SpaStaticFilesExtensions.cs +++ b/src/Middleware/SpaServices.Extensions/src/StaticFiles/SpaStaticFilesExtensions.cs @@ -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); }