using Microsoft.AspNetCore.Builder; using Swashbuckle.AspNetCore.SwaggerUI; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using WIDESEAWCS_Core.Helper; using static WIDESEAWCS_Core.Extensions.CustomApiVersion; namespace WIDESEAWCS_Core.Middlewares { /// <summary> /// Swaggerä¸é—´ä»¶ /// </summary> public static class SwaggerMiddleware { public static void UseSwaggerMiddle(this IApplicationBuilder app, Func<Stream> streamHtml) { if (app == null) throw new ArgumentNullException(nameof(app)); app.UseSwagger(); app.UseSwaggerUI(c => { //æ ¹æ®ç‰ˆæœ¬åç§°å€’åº é历展示 var apiName = AppSettings.app(new string[] { "ApiName" }); typeof(ApiVersions).GetEnumNames().OrderByDescending(e => e).ToList().ForEach(version => { c.SwaggerEndpoint($"/swagger/{version}/swagger.json", $"{apiName} {version}"); }); //c.SwaggerEndpoint("/swagger/v1/swagger.json", "WIDESEA.CoreåŽå°Api"); //c.SwaggerEndpoint($"https://petstore.swagger.io/v2/swagger.json", $"{apiName} pet"); // å°†swaggeré¦–é¡µï¼Œè®¾ç½®æˆæˆ‘们自定义的页é¢ï¼Œè®°å¾—这个å—符串的写法:{项目å.index.html} if (streamHtml.Invoke() == null) { var msg = "index.html的属性,必须设置为嵌入的资æº"; //Log.Error(msg); throw new Exception(msg); } c.IndexStream = streamHtml; c.DocExpansion(DocExpansion.None); //->ä¿®æ”¹ç•Œé¢æ‰“开时自动折å //if (Permissions.IsUseIds4) //{ // c.OAuthClientId("blogadminjs"); //} // 路径é…ç½®ï¼Œè®¾ç½®ä¸ºç©ºï¼Œè¡¨ç¤ºç›´æŽ¥åœ¨æ ¹åŸŸå(localhost:8001)访问该文件,注æ„localhost:8001/swagger是访问ä¸åˆ°çš„,去launchSettings.json把launchUrlåŽ»æŽ‰ï¼Œå¦‚æžœä½ æƒ³æ¢ä¸€ä¸ªè·¯å¾„,直接写åå—å³å¯ï¼Œæ¯”如直接写c.RoutePrefix = "doc"; c.RoutePrefix = ""; }); } } }