From ccc1a2d6302df5b9800390ffc4d1f980853bac22 Mon Sep 17 00:00:00 2001 From: Lei Li Date: Mon, 30 Jul 2018 16:34:42 +1000 Subject: [PATCH] Add RoutePrefix for Swagger UI and Swagger docs. This change support the scenario that there are multiple swaggers used by ASP.NET web API projects as different Areas in the ASP.NET web MVC project. --- .../HttpConfigurationExtensions.cs | 61 +++++++++++++------ .../Application/SwaggerDocsConfig.cs | 3 + .../Application/SwaggerUiConfig.cs | 17 ++++-- 3 files changed, 55 insertions(+), 26 deletions(-) diff --git a/Swashbuckle.Core/Application/HttpConfigurationExtensions.cs b/Swashbuckle.Core/Application/HttpConfigurationExtensions.cs index 489b87110..074372ede 100644 --- a/Swashbuckle.Core/Application/HttpConfigurationExtensions.cs +++ b/Swashbuckle.Core/Application/HttpConfigurationExtensions.cs @@ -14,20 +14,24 @@ public static class HttpConfigurationExtensions { private static readonly string DefaultRouteTemplate = "swagger/docs/{apiVersion}"; - public static SwaggerEnabledConfiguration EnableSwagger( - this HttpConfiguration httpConfig, - Action configure = null) + public static SwaggerEnabledConfiguration EnableSwagger(this HttpConfiguration httpConfig, Action configure = null) { return EnableSwagger(httpConfig, DefaultRouteTemplate, configure); } - public static SwaggerEnabledConfiguration EnableSwagger( - this HttpConfiguration httpConfig, - string routeTemplate, - Action configure = null) + public static SwaggerEnabledConfiguration EnableSwagger(this HttpConfiguration httpConfig, string routeTemplate, Action configure = null) { - var config = new SwaggerDocsConfig(); - if (configure != null) configure(config); + var config = new SwaggerDocsConfig(); + + if (configure != null) + { + configure(config); + } + + if (!string.IsNullOrWhiteSpace(config.RoutePrefix)) + { + routeTemplate = config.RoutePrefix + "/" + routeTemplate; + } httpConfig.Routes.MapHttpRoute( name: "swagger_docs" + routeTemplate, @@ -76,20 +80,26 @@ public void EnableSwaggerUi(Action configure = null) EnableSwaggerUi(DefaultRouteTemplate, configure); } - public void EnableSwaggerUi( - string routeTemplate, - Action configure = null) + public void EnableSwaggerUi(string routeTemplate, Action configure = null) { var config = new SwaggerUiConfig(_discoveryPaths, _rootUrlResolver); - if (configure != null) configure(config); - _httpConfig.Routes.MapHttpRoute( - name: "swagger_ui" + routeTemplate, - routeTemplate: routeTemplate, - defaults: null, - constraints: new { assetPath = @".+" }, - handler: new SwaggerUiHandler(config) - ); + if (configure != null) + { + configure(config); + } + + if (!string.IsNullOrWhiteSpace(config.RoutePrefix)) + { + routeTemplate = config.RoutePrefix + "/" + routeTemplate; + } + + _httpConfig.Routes.MapHttpRoute( + name: "swagger_ui" + routeTemplate, + routeTemplate: routeTemplate, + defaults: null, + constraints: new { assetPath = @".+" }, + handler: new SwaggerUiHandler(config)); if (routeTemplate == DefaultRouteTemplate) { @@ -100,6 +110,17 @@ public void EnableSwaggerUi( constraints: new { uriResolution = new HttpRouteDirectionConstraint(HttpRouteDirection.UriResolution) }, handler: new RedirectHandler(_rootUrlResolver, "swagger/ui/index")); } + + if (routeTemplate == config.RoutePrefix + "/" + DefaultRouteTemplate) + { + _httpConfig.Routes.MapHttpRoute( + name: "swagger_ui_shortcut" + routeTemplate, + routeTemplate: config.RoutePrefix + "/" + "swagger", + defaults: null, + constraints: new { uriResolution = new HttpRouteDirectionConstraint(HttpRouteDirection.UriResolution) }, + handler: new RedirectHandler(_rootUrlResolver, config.RoutePrefix + "/" + "swagger/ui/index")); + } + } } } \ No newline at end of file diff --git a/Swashbuckle.Core/Application/SwaggerDocsConfig.cs b/Swashbuckle.Core/Application/SwaggerDocsConfig.cs index 64d21e080..34a8cd6e9 100644 --- a/Swashbuckle.Core/Application/SwaggerDocsConfig.cs +++ b/Swashbuckle.Core/Application/SwaggerDocsConfig.cs @@ -40,6 +40,9 @@ public class SwaggerDocsConfig private Func _customProviderFactory; + // Routing Prefix to the Swagger Docs + public string RoutePrefix { set; get; } + public SwaggerDocsConfig() { _versionInfoBuilder = new VersionInfoBuilder(); diff --git a/Swashbuckle.Core/Application/SwaggerUiConfig.cs b/Swashbuckle.Core/Application/SwaggerUiConfig.cs index f5e75aab8..059aaa9ad 100644 --- a/Swashbuckle.Core/Application/SwaggerUiConfig.cs +++ b/Swashbuckle.Core/Application/SwaggerUiConfig.cs @@ -14,6 +14,10 @@ public class SwaggerUiConfig private readonly Dictionary _templateParams; private readonly Func _rootUrlResolver; + // Routing Prefix to the Swagger UI + public string RoutePrefix { set; get; } + + public SwaggerUiConfig(IEnumerable discoveryPaths, Func rootUrlResolver) { _pathToAssetMap = new Dictionary(); @@ -34,8 +38,8 @@ public SwaggerUiConfig(IEnumerable discoveryPaths, Func