Yuebon.Extensions/ServiceExtensions/AddSwaggerSetup.cs

(开头部分) 5KB

这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Versioning;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.Filters;
using System.Runtime.InteropServices;
using Yuebon.Commons.Filters;
using Yuebon.Commons.Log;
using static Yuebon.Extensions.ServiceExtensions.SwaggerVersions;

namespace Yuebon.Extensions.ServiceExtensions;

/// <summary>
/// Swagger 启动服务
/// </summary>
public static class SwaggerSetup
{

    private static readonly ILog log =LogManager.GetLogger(typeof(SwaggerSetup));
    /// <summary>
    /// 
    /// </summary>
    /// <param name="services"></param>
    /// <exception cref="ArgumentNullException"></exception>
    public static void AddSwaggerSetup(this IServiceCollection services)
    {
        if (services == null) throw new ArgumentNullException(nameof(services));
        // Api多版本版本配置
        services.AddApiVersioning(o =>
        {
            o.ReportApiVersions = true;//是否在请求头中返回受支持的版本信息。
            o.AssumeDefaultVersionWhenUnspecified = true;//请求没有指明版本的情况下是否使用默认的版本。
            o.DefaultApiVersion = new ApiVersion(1, 0);//默认的版本号。
            o.ApiVersionReader = ApiVersionReader.Combine(new HeaderApiVersionReader("api-version"));////版本信息放到header ,不写在不配置路由的情况下,版本信息放到response url 中
        }).AddVersionedApiExplorer();

        services.AddSwaggerGen(options =>
        {
            string contactName = Appsettings.Configuration.GetSection("SwaggerDoc:ContactName").Value;
            string contactNameEmail = Appsettings.Configuration.GetSection("SwaggerDoc:ContactEmail").Value;
            string contactUrl = Appsettings.Configuration.GetSection("SwaggerDoc:ContactUrl").Value;
            string termsOfService= Appsettings.Configuration.GetSection("SwaggerDoc:TermsOfService").Value;

            //遍历出全部的版本,做文档信息展示
            typeof(ApiVersions).GetEnumNames().ToList().ForEach(version =>
            {
                options.SwaggerDoc(version,
                     new OpenApiInfo()
                     {
                         Title = $"{Appsettings.Configuration.GetSection("SwaggerDoc:Title").Value}-{RuntimeInformation.FrameworkDescription}",
                         Version = $"{ version}",
                         TermsOfService = new Uri(termsOfService),
                         Description = Appsettings.Configuration.GetSection("SwaggerDoc:Description").Value,
                         Contact = new OpenApiContact { Name = contactName, Email = contactNameEmail, Url = new Uri(contactUrl) },
                         License = new OpenApiLicense { Name = "Apache 2.0", Url = new Uri("https://www.apache.org/licenses/LICENSE-2.0") }
                     }
                ); ;
            });
            //options.IgnoreObsoleteActions();//忽略掉废弃接接口,即不展示
            //options.IgnoreObsoleteProperties();//废弃接接口
            options.UseInlineDefinitionsForEnums();
后面还有 81 行代码,解锁后查看完整代码

24 小时内免费解锁 3 个项目,之后 1 算力/个。 规则说明

AI 解读 AI

登录后可用,每次 10 算力,解读结果公开显示在下面。

还没有人解读过这个文件。