mirror of https://github.com/Squidex/squidex.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
47 lines
1.5 KiB
47 lines
1.5 KiB
// ==========================================================================
|
|
// Squidex Headless CMS
|
|
// ==========================================================================
|
|
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|
// All rights reserved. Licensed under the MIT license.
|
|
// ==========================================================================
|
|
|
|
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Squidex.Hosting;
|
|
|
|
namespace Squidex.Web;
|
|
|
|
public static class PathBaseMiddlewareExtensions
|
|
{
|
|
public static IApplicationBuilder UseFallbackPathBase(this IApplicationBuilder app)
|
|
{
|
|
var urlGenerator = app.ApplicationServices.GetRequiredService<IUrlGenerator>();
|
|
|
|
var configuredBase = urlGenerator.BuildBasePath();
|
|
if (string.IsNullOrWhiteSpace(configuredBase) || configuredBase == "/")
|
|
{
|
|
return app;
|
|
}
|
|
|
|
var pathBase = new PathString(configuredBase);
|
|
|
|
return app.Use(async (context, next) =>
|
|
{
|
|
if (context.Request.PathBase.HasValue)
|
|
{
|
|
await next();
|
|
return;
|
|
}
|
|
|
|
context.Request.PathBase = pathBase;
|
|
|
|
if (context.Request.Path.StartsWithSegments(pathBase, StringComparison.Ordinal, out var remainder))
|
|
{
|
|
context.Request.Path = remainder;
|
|
}
|
|
|
|
await next();
|
|
});
|
|
}
|
|
}
|
|
|