Asp.net6.0?Swagger使用問(wèn)題及解決過(guò)程
“五一”期間用了一下Swagger,碰到了以下問(wèn)題:
- 如何在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)用說(shuō)明", Description = "巴拉巴拉的一堆描述", License = new OpenApiLicense { Name = "Api調(diào)用須知", Url = new Uri("http://xxx.com/readmi.html") } }); });
以上設(shè)置的效果
然而發(fā)布到Docker以后,中文亂碼了(似乎在Program.cs里面輸入的中文都會(huì)亂碼)。我的解決的辦法是: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)用說(shuō)明", Description = "巴拉巴拉的一堆描述", License = new OpenApiLicense { Name = "Api調(diào)用須知", Url = new Uri("http://xxx.com/readmi.html") } }); } }
問(wèn)題解決,但有點(diǎn)脫褲子放屁,不知道有沒(méi)有不需要脫褲子的方法。
二、顯示xml注釋
這個(gè)微軟文檔說(shuō)的很清楚,這里記一下網(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 = "我的簽名是這個(gè)生成的,巴拉巴拉巴拉", Required = true }); } } }
然后在SwaggerSetup里面添加一行options.OperationFilter<SwaggerOperationFilter>();
在ApiController或者Action前面添加一行[SwaggerHeader]
四、隱藏ApiController、Action、類或者屬性,顯示枚舉
想把1、2隱藏起來(lái),把
public enum ECode { 成功, [Description("時(shí)間戳錯(cuò)誤")] Timestamp, [Description("簽名錯(cuò)誤")] 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特性的類作個(gè)標(biāo)記,以便在DocumentFilter里面移除整個(gè)架構(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á)到了我的目的,但存兩點(diǎn)不爽:
- 脫褲子
- 隱藏類的方式
到此這篇關(guān)于Asp.net6.0 Swagger使用備忘的文章就介紹到這了,更多相關(guān)Asp.net6.0 Swagger使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Asp.net中使用DapperExtensions和反射來(lái)實(shí)現(xiàn)一個(gè)通用搜索
這篇文章主要介紹了Asp.net中使用DapperExtensions和反射來(lái)實(shí)現(xiàn)一個(gè)通用搜索功能,非常不錯(cuò),具有參考解決價(jià)值,需要的朋友可以參考下2017-03-03Asp.net利用JQuery AJAX實(shí)現(xiàn)無(wú)刷新評(píng)論思路與代碼
Asp.net利用JQuery AJAX實(shí)現(xiàn)無(wú)刷新評(píng)論,此功能是每一個(gè)從事asp.net開(kāi)發(fā)者的朋友都希望實(shí)現(xiàn)的,本文利用閑暇時(shí)間整理了一些,有需要的朋友可以參考下2012-12-12IIS中ASP.NET連接SQL Server出錯(cuò)的解決方法
在IIS中運(yùn)行的ASP.NET應(yīng)用程序其所屬用戶名為ASPNET的特定用戶,其默認(rèn)權(quán)限是無(wú)法訪問(wèn)SQL Server的,更不可能訪問(wèn)ASP.NET應(yīng)用程序的數(shù)據(jù)庫(kù)了,因此要在IIS中訪問(wèn)SQL Server就需要給ASPNET帳戶賦予相應(yīng)的權(quán)限.2010-03-03DataGridView中綁定DataTable數(shù)據(jù)及相關(guān)操作實(shí)現(xiàn)代碼
DataGridView中綁定DataTable數(shù)據(jù)及相關(guān)操作2010-02-02asp.net實(shí)現(xiàn)word文檔在線預(yù)覽功能的方法
這篇文章主要介紹了asp.net實(shí)現(xiàn)word文檔在線預(yù)覽功能的方法,可實(shí)現(xiàn)office文檔轉(zhuǎn)html,再在瀏覽器里面在線瀏覽,是非常實(shí)用的技巧,需要的朋友可以參考下2014-11-11ASP.NET漢字轉(zhuǎn)拼音 - 輸入漢字獲取其拼音的具體實(shí)現(xiàn)
這篇文章主要介紹了ASP.NET漢字轉(zhuǎn)拼音 - 輸入漢字獲取其拼音的具體實(shí)現(xiàn),需要的朋友可以參考下2014-02-02ASP.NET Mvc開(kāi)發(fā)之刪除修改數(shù)據(jù)
這篇文章主要介紹了ASP.NET Mvc開(kāi)發(fā)中的刪除修改數(shù)據(jù)功能,感興趣的小伙伴們可以參考一下2016-03-03ASP.NET MVC 開(kāi)發(fā)微信支付H5的實(shí)現(xiàn)示例(外置瀏覽器支付)
這篇文章主要介紹了ASP.NET MVC 開(kāi)發(fā)微信支付H5的實(shí)現(xiàn)示例(外置瀏覽器支付),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12WPF實(shí)現(xiàn)定時(shí)刷新UI界面功能
這篇文章主要為大家詳細(xì)介紹了WPF實(shí)現(xiàn)定時(shí)刷新UI界面功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07asp.net使用DataSet的ReadXml讀取XML文件及Stream流的方法
這篇文章主要介紹了asp.net使用DataSet的ReadXml讀取XML文件及Stream流的方法,實(shí)例分析了asp.net以字符流的形式讀取與寫入xml文件的相關(guān)技巧,需要的朋友可以參考下2016-06-06