diff --git a/framework/src/Volo.Abp.Swagger/Microsoft/AspNetCore/Builder/AbpSwaggerUIBuilderExtensions.cs b/framework/src/Volo.Abp.Swagger/Microsoft/AspNetCore/Builder/AbpSwaggerUIBuilderExtensions.cs index 243a8cebb3..f562dfb202 100644 --- a/framework/src/Volo.Abp.Swagger/Microsoft/AspNetCore/Builder/AbpSwaggerUIBuilderExtensions.cs +++ b/framework/src/Volo.Abp.Swagger/Microsoft/AspNetCore/Builder/AbpSwaggerUIBuilderExtensions.cs @@ -1,7 +1,7 @@ using System; using Microsoft.Extensions.DependencyInjection; using Swashbuckle.AspNetCore.SwaggerUI; -using Volo.Abp.VirtualFileSystem; +using Volo.Abp; namespace Microsoft.AspNetCore.Builder { @@ -11,11 +11,13 @@ namespace Microsoft.AspNetCore.Builder this IApplicationBuilder app, Action setupAction = null) { - var fileProvider = app.ApplicationServices.GetService(); + var resolver = app.ApplicationServices.GetService(); return app.UseSwaggerUI(options => { - options.IndexStream = () => fileProvider.GetFileInfo("/wwwroot/swagger/ui/index.html").CreateReadStream(); + options.InjectJavascript("/libs/abp/core/abp.js"); + options.InjectJavascript("/swagger/ui/abp.swagger.js"); + options.IndexStream = () => resolver.Resolver(); setupAction?.Invoke(options); }); diff --git a/framework/src/Volo.Abp.Swagger/Volo.Abp.Swagger.csproj b/framework/src/Volo.Abp.Swagger/Volo.Abp.Swagger.csproj index e9f4287b30..62c580f0b5 100644 --- a/framework/src/Volo.Abp.Swagger/Volo.Abp.Swagger.csproj +++ b/framework/src/Volo.Abp.Swagger/Volo.Abp.Swagger.csproj @@ -18,12 +18,12 @@ - - + - + + diff --git a/framework/src/Volo.Abp.Swagger/Volo/Abp/ISwaggerHtmlResolver.cs b/framework/src/Volo.Abp.Swagger/Volo/Abp/ISwaggerHtmlResolver.cs new file mode 100644 index 0000000000..f22a03268c --- /dev/null +++ b/framework/src/Volo.Abp.Swagger/Volo/Abp/ISwaggerHtmlResolver.cs @@ -0,0 +1,9 @@ +using System.IO; + +namespace Volo.Abp +{ + public interface ISwaggerHtmlResolver + { + Stream Resolver(); + } +} diff --git a/framework/src/Volo.Abp.Swagger/Volo/Abp/SwaggerHtmlResolver.cs b/framework/src/Volo.Abp.Swagger/Volo/Abp/SwaggerHtmlResolver.cs new file mode 100644 index 0000000000..e447ddad16 --- /dev/null +++ b/framework/src/Volo.Abp.Swagger/Volo/Abp/SwaggerHtmlResolver.cs @@ -0,0 +1,23 @@ +using System.IO; +using System.Reflection; +using System.Text; +using Swashbuckle.AspNetCore.SwaggerUI; +using Volo.Abp.DependencyInjection; + +namespace Volo.Abp +{ + public class SwaggerHtmlResolver : ISwaggerHtmlResolver, ITransientDependency + { + public virtual Stream Resolver() + { + var stream = typeof(SwaggerUIOptions).GetTypeInfo().Assembly + .GetManifestResourceStream("Swashbuckle.AspNetCore.SwaggerUI.index.html"); + + var html = new StreamReader(stream) + .ReadToEnd() + .Replace("SwaggerUIBundle(configObject)", "AbpSwaggerUIBundle(configObject)"); + + return new MemoryStream(Encoding.UTF8.GetBytes(html)); + } + } +} diff --git a/framework/src/Volo.Abp.Swagger/wwwroot/swagger/ui/abp.swagger.js b/framework/src/Volo.Abp.Swagger/wwwroot/swagger/ui/abp.swagger.js new file mode 100644 index 0000000000..758a7fc959 --- /dev/null +++ b/framework/src/Volo.Abp.Swagger/wwwroot/swagger/ui/abp.swagger.js @@ -0,0 +1,14 @@ +function AbpSwaggerUIBundle(configObject) { + configObject.requestInterceptor = function (request) { + var token = abp.auth.getToken(); + request.headers.Authorization = token ? "Bearer " + token : null; + var antiForgeryToken = abp.security.antiForgery.getToken(); + if (antiForgeryToken) { + request.headers[abp.security.antiForgery.tokenHeaderName] = antiForgeryToken; + } + return request; + }; + + return SwaggerUIBundle(configObject); +} + diff --git a/framework/src/Volo.Abp.Swagger/wwwroot/swagger/ui/index.html b/framework/src/Volo.Abp.Swagger/wwwroot/swagger/ui/index.html deleted file mode 100644 index 9dd9371ab0..0000000000 --- a/framework/src/Volo.Abp.Swagger/wwwroot/swagger/ui/index.html +++ /dev/null @@ -1,95 +0,0 @@ - - - - - - %(DocumentTitle) - - - - - - %(HeadContent) - - - - -
- - - - - - - - - - - - diff --git a/nupkg/common.ps1 b/nupkg/common.ps1 index f96ee7484b..bbf73de7bb 100644 --- a/nupkg/common.ps1 +++ b/nupkg/common.ps1 @@ -136,6 +136,7 @@ $projects = ( "framework/src/Volo.Abp.Validation", "framework/src/Volo.Abp.VirtualFileSystem", "framework/src/Volo.Abp.Kafka", + "framework/src/Volo.Abp.Swagger", # modules/account "modules/account/src/Volo.Abp.Account.Application.Contracts", diff --git a/templates/module/aspnet-core/host/MyCompanyName.MyProjectName.Web.Host/MyProjectNameWebHostModule.cs b/templates/module/aspnet-core/host/MyCompanyName.MyProjectName.Web.Host/MyProjectNameWebHostModule.cs index f072732f06..06e65a1192 100644 --- a/templates/module/aspnet-core/host/MyCompanyName.MyProjectName.Web.Host/MyProjectNameWebHostModule.cs +++ b/templates/module/aspnet-core/host/MyCompanyName.MyProjectName.Web.Host/MyProjectNameWebHostModule.cs @@ -249,7 +249,7 @@ namespace MyCompanyName.MyProjectName app.UseSwagger(); app.UseAbpSwaggerUI(options => { - options.SwaggerEndpoint("/swagger/v1/swagger.json", "Support APP API"); + options.SwaggerEndpoint("/swagger/v1/swagger.json", "MyProjectName API"); }); app.UseAuditing();