欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Asp.net6.0?Swagger使用問題及解決過程

 更新時間:2022年05月05日 14:37:04   作者:毛毛蟲  
這篇文章主要介紹了Asp.net6.0?Swagger使用備忘,文中介紹了在Docker中顯示OpenApiInfo的中文內(nèi)容,顯示xml注釋及如何顯示Header的問題,需要的朋友可以參考下

“五一”期間用了一下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)文章

最新評論