Asp.net6.0?Swagger使用問題及解決過程
“五一”期間用了一下Swagger,碰到了以下問題:
- 如何在Docker中顯示OpenApiInfo的中文內(nèi)容;
- 如何顯示xml注釋;
- 如何顯示Header;
- 如何隱藏ApiController、Action、類或者屬性,如何顯示枚舉
現(xiàn)將解決辦法記下留存。
一、在Docker中顯示OpenApiInfo的中文內(nèi)容
builder.Services.AddSwaggerGen(options => { options.SwaggerDoc("v1", new OpenApiInfo { Version = "v1", Title = "xxx Api調(diào)用說明", Description = "巴拉巴拉的一堆描述", License = new OpenApiLicense { Name = "Api調(diào)用須知", Url = new Uri("http://xxx.com/readmi.html") } }); });
以上設(shè)置的效果
然而發(fā)布到Docker以后,中文亂碼了(似乎在Program.cs里面輸入的中文都會亂碼)。我的解決的辦法是:builder.Services.AddSwaggerGen(ProgramHelper.SwaggerSetup);
using Microsoft.OpenApi.Models; using Swashbuckle.AspNetCore.SwaggerGen; namespace SwaggerDemo; public static class ProgramHelper { public static void SwaggerSetup(SwaggerGenOptions options) { options.SwaggerDoc("v1", new OpenApiInfo { Version = "v1", Title = "xxx Api調(diào)用說明", Description = "巴拉巴拉的一堆描述", License = new OpenApiLicense { Name = "Api調(diào)用須知", Url = new Uri("http://xxx.com/readmi.html") } }); } }
問題解決,但有點脫褲子放屁,不知道有沒有不需要脫褲子的方法。
二、顯示xml注釋
這個微軟文檔說的很清楚,這里記一下網(wǎng)址備查。
三、如何顯示Header
辦法如下:
public class SwaggerHeaderAttribute : Attribute { } public class SwaggerOperationFilter : IOperationFilter { public void Apply(OpenApiOperation operation, OperationFilterContext context) { if (context.ApiDescription.CustomAttributes().Any(ii => ii.GetType() == typeof(SwaggerHeaderAttribute)) || context.ApiDescription.ActionDescriptor.EndpointMetadata.Any(ii => ii.GetType() == typeof(SwaggerHeaderAttribute))) { if (operation.Parameters == null) operation.Parameters = new List<OpenApiParameter>(); operation.Parameters.Add(new OpenApiParameter { Name = "Sign", In = ParameterLocation.Header, Description = "我的簽名是這個生成的,巴拉巴拉巴拉", Required = true }); } } }
然后在SwaggerSetup里面添加一行options.OperationFilter<SwaggerOperationFilter>();
在ApiController或者Action前面添加一行[SwaggerHeader]
四、隱藏ApiController、Action、類或者屬性,顯示枚舉
想把1、2隱藏起來,把
public enum ECode { 成功, [Description("時間戳錯誤")] Timestamp, [Description("簽名錯誤")] Sign }
顯示成3的樣子,辦法如下:
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Enum | AttributeTargets.Field)] public partial class SwaggerIgnoreAttribute : Attribute { } public class SwaggerSchemaFilter : ISchemaFilter { public void Apply(OpenApiSchema schema, SchemaFilterContext context) { #region 給設(shè)置了SwaggerIgnoreAttribute特性的類作個標(biāo)記,以便在DocumentFilter里面移除整個架構(gòu) if (context.Type.GetCustomAttribute<SwaggerIgnoreAttribute>() != null) { schema.Title = "Remove"; } #endregion else { if (context.Type.IsEnum) { #region 設(shè)置枚舉的描述 List<string> titleItems = new List<string>(); foreach (var e in Enum.GetValues(context.Type)) { if (context.Type.GetField(e.ToString()).GetCustomAttribute<SwaggerIgnoreAttribute>() == null) { titleItems.Add($"{(int)e}:{context.Type.GetField(e.ToString()).GetCustomAttribute<DescriptionAttribute>()?.Description ?? e}"); } } schema.Description = string.Join(";", titleItems); #endregion } else { #region 移除設(shè)置了SwaggerIgnoreAttribute特性的屬性 foreach (var propertyName in context.Type.GetProperties().Where(ii => ii.GetCustomAttribute<SwaggerIgnoreAttribute>() != null).Select(ii => ii.Name)) schema.Properties.Remove(propertyName); #region } } } } public class SwaggerDocFilter : IDocumentFilter { public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context) { //移除在SchemaFilter作了標(biāo)記的架構(gòu) foreach (var key in swaggerDoc.Components.Schemas.Where(ii => ii.Value.Title == "Remove")) swaggerDoc.Components.Schemas.Remove(key); #region 移除設(shè)置了SwaggerIgnoreAttribute特性的ApiController var ignoreApis = context.ApiDescriptions.Where(wh => wh.ActionDescriptor.EndpointMetadata.Any(any => any is SwaggerIgnoreAttribute)); if (ignoreApis != null) { foreach (var ignoreApi in ignoreApis) { swaggerDoc.Paths.Remove("/" + ignoreApi.RelativePath); } } #endregion } }
照例需要在在SwaggerSetup里面添加兩行options.SchemaFilter<SwaggerSchemaFilter>();
options.DocumentFilter<SwaggerDocFilter>();
然后給隱藏的內(nèi)容添加[SwaggerIgnore]
如此,大致達(dá)到了我的目的,但存兩點不爽:
- 脫褲子
- 隱藏類的方式
到此這篇關(guān)于Asp.net6.0 Swagger使用備忘的文章就介紹到這了,更多相關(guān)Asp.net6.0 Swagger使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Asp.net中使用DapperExtensions和反射來實現(xiàn)一個通用搜索
這篇文章主要介紹了Asp.net中使用DapperExtensions和反射來實現(xiàn)一個通用搜索功能,非常不錯,具有參考解決價值,需要的朋友可以參考下2017-03-03Asp.net利用JQuery AJAX實現(xiàn)無刷新評論思路與代碼
Asp.net利用JQuery AJAX實現(xiàn)無刷新評論,此功能是每一個從事asp.net開發(fā)者的朋友都希望實現(xiàn)的,本文利用閑暇時間整理了一些,有需要的朋友可以參考下2012-12-12IIS中ASP.NET連接SQL Server出錯的解決方法
在IIS中運行的ASP.NET應(yīng)用程序其所屬用戶名為ASPNET的特定用戶,其默認(rèn)權(quán)限是無法訪問SQL Server的,更不可能訪問ASP.NET應(yīng)用程序的數(shù)據(jù)庫了,因此要在IIS中訪問SQL Server就需要給ASPNET帳戶賦予相應(yīng)的權(quán)限.2010-03-03DataGridView中綁定DataTable數(shù)據(jù)及相關(guān)操作實現(xiàn)代碼
DataGridView中綁定DataTable數(shù)據(jù)及相關(guān)操作2010-02-02asp.net實現(xiàn)word文檔在線預(yù)覽功能的方法
這篇文章主要介紹了asp.net實現(xiàn)word文檔在線預(yù)覽功能的方法,可實現(xiàn)office文檔轉(zhuǎn)html,再在瀏覽器里面在線瀏覽,是非常實用的技巧,需要的朋友可以參考下2014-11-11ASP.NET漢字轉(zhuǎn)拼音 - 輸入漢字獲取其拼音的具體實現(xiàn)
這篇文章主要介紹了ASP.NET漢字轉(zhuǎn)拼音 - 輸入漢字獲取其拼音的具體實現(xiàn),需要的朋友可以參考下2014-02-02ASP.NET Mvc開發(fā)之刪除修改數(shù)據(jù)
這篇文章主要介紹了ASP.NET Mvc開發(fā)中的刪除修改數(shù)據(jù)功能,感興趣的小伙伴們可以參考一下2016-03-03ASP.NET MVC 開發(fā)微信支付H5的實現(xiàn)示例(外置瀏覽器支付)
這篇文章主要介紹了ASP.NET MVC 開發(fā)微信支付H5的實現(xiàn)示例(外置瀏覽器支付),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12asp.net使用DataSet的ReadXml讀取XML文件及Stream流的方法
這篇文章主要介紹了asp.net使用DataSet的ReadXml讀取XML文件及Stream流的方法,實例分析了asp.net以字符流的形式讀取與寫入xml文件的相關(guān)技巧,需要的朋友可以參考下2016-06-06