Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(pathbase): Add support for app.UsePathBase #2615

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
image: Visual Studio 2019
image: Visual Studio 2022

install:
- ps: Invoke-WebRequest "https://dot.net/v1/dotnet-install.ps1" -OutFile "./dotnet-install.ps1"
Expand Down
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<RepositoryType>git</RepositoryType>
<RepositoryUrl>https://github.com/domaindrivendev/Swashbuckle.AspNetCore.git</RepositoryUrl>
<VersionPrefix>6.5.0</VersionPrefix>
<VersionPrefix>6.6.0</VersionPrefix>
<LangVersion>9</LangVersion>
</PropertyGroup>

Expand Down
28 changes: 25 additions & 3 deletions src/Swashbuckle.AspNetCore.SwaggerUI/SwaggerUIMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,33 @@ public class SwaggerUIMiddleware

public async Task Invoke(HttpContext httpContext)
{
if (!string.IsNullOrWhiteSpace(_options.PathBase)
&& (!httpContext.Request.PathBase.HasValue
|| !httpContext.Request.PathBase.Value.Equals(_options.PathBase, StringComparison.OrdinalIgnoreCase)))
{
await _staticFileMiddleware.Invoke(httpContext);
return;
}

var httpMethod = httpContext.Request.Method;
var path = httpContext.Request.Path.Value;
string path;
string routePrefix;

if (!string.IsNullOrWhiteSpace(_options.PathBase)
&& httpContext.Request.PathBase.HasValue
&& httpContext.Request.PathBase.Value.Equals(_options.PathBase, StringComparison.OrdinalIgnoreCase))
{
path = string.Join("/", _options.PathBase.TrimEnd('/'), httpContext.Request.Path.Value.Trim('/'));
routePrefix = string.Join("/", _options.PathBase.Trim('/'), _options.RoutePrefix.Trim('/'));
}
else
{
path = httpContext.Request.Path.Value;
routePrefix = _options.RoutePrefix;
}

// If the RoutePrefix is requested (with or without trailing slash), redirect to index URL
if (httpMethod == "GET" && Regex.IsMatch(path, $"^/?{Regex.Escape(_options.RoutePrefix)}/?$", RegexOptions.IgnoreCase))
if (httpMethod == "GET" && Regex.IsMatch(path, $"^/?{Regex.Escape(routePrefix)}/?$", RegexOptions.IgnoreCase))
{
// Use relative redirect to support proxy environments
var relativeIndexUrl = string.IsNullOrEmpty(path) || path.EndsWith("/")
Expand All @@ -68,7 +90,7 @@ public async Task Invoke(HttpContext httpContext)
return;
}

if (httpMethod == "GET" && Regex.IsMatch(path, $"^/{Regex.Escape(_options.RoutePrefix)}/?index.html$", RegexOptions.IgnoreCase))
if (httpMethod == "GET" && Regex.IsMatch(path, $"^/{Regex.Escape(routePrefix)}/?index.html$", RegexOptions.IgnoreCase))
{
await RespondWithIndexHtml(httpContext.Response);
return;
Expand Down
6 changes: 6 additions & 0 deletions src/Swashbuckle.AspNetCore.SwaggerUI/SwaggerUIOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ namespace Swashbuckle.AspNetCore.SwaggerUI
{
public class SwaggerUIOptions
{
/// <summary>
/// Gets or sets the path base, as in app.UsePathBase()
/// </summary>
public string PathBase { get; set; } = "";


/// <summary>
/// Gets or sets a route prefix for accessing the swagger-ui
/// </summary>
Expand Down