zoukankan      html  css  js  c++  java
  • net5集成swagger

    net5集成swagger

    1、新建站点,net5版本

    2、通过nuget引用Swashbuckle.AspNetCore

    3、添加自定义类

    /// <summary>
        /// swagger
        /// </summary>
        public static class SwaggerExtension
        {
            public static void AddSwaggerInfo(this IServiceCollection services)
            {
                if (services == null) throw new ArgumentNullException(nameof(services));
    
                var ApiName = "mytestapi";
    
                services.AddSwaggerGen(c =>
                {
                    c.SwaggerDoc("V1", new OpenApiInfo
                    {
                        Version = "V1",
                        Title = $"{ApiName} 接口文档——mytestapi",
                        Description = $"{ApiName} HTTP API V1",
    
                    });
                    c.OrderActionsBy(o => o.RelativePath);
    
                    var security = new OpenApiSecurityRequirement()
                    {
                        { new OpenApiSecurityScheme
                        {
                            Reference = new OpenApiReference()
                            {
                                Id = "my-token-key",
                                Type = ReferenceType.SecurityScheme
                            }
                        }, Array.Empty<string>() }
                    };
                    c.AddSecurityRequirement(security);//添加一个必须的全局安全信息,和AddSecurityDefinition方法指定的方案名称要一致,这里是Bearer。
                    c.AddSecurityDefinition("my-token-key", new OpenApiSecurityScheme
                    {
                        Description = "JWT",
                        Name = "mytokenkey",
                        In = ParameterLocation.Header,
                        Type = SecuritySchemeType.ApiKey
                    });
    
                    // 获取xml
                    var xmlPath = Path.Combine(AppContext.BaseDirectory, "mytestapi.xml");
                    var mxmlPath = Path.Combine(AppContext.BaseDirectory, "mytestapimodel.xml");
                    c.IncludeXmlComments(xmlPath, true);
                    c.IncludeXmlComments(mxmlPath);
                });
    
            }
        }

    4、在ConfigServices中引用

    services.AddSwaggerInfo();

    5、在Config中增加:

    app.UseSwagger();
                app.UseSwaggerUI(c =>
                {
                    c.SwaggerEndpoint($"/swagger/V1/swagger.json", "mytestapi V1");
    
                    c.RoutePrefix = “”;//前缀
                    
                    c.DefaultModelsExpandDepth(0);
    
                });

    6、最后记得选择生成xml文件

    运行效果:

    路径为站点路径,带上/index.html。如果有前缀的,则带上/前缀/index.html

  • 相关阅读:
    CVE-2017-11826:Office Open XML 标签嵌套解析混淆漏洞
    未授权访问漏洞总结
    Linux提权—脏牛漏洞(CVE-2016-5195)
    Linux提权
    (翻译)OpenDocument and Open XML security (OpenOffice.org and MS Office 2007)
    SSH后门万能密码
    Linux中使用gdb dump内存
    在 x64dbg 中设置条件断点和条件记录断点
    Linux中的.bash_ 文件详解
    Photoshop 第二课 工具-钢笔的使用
  • 原文地址:https://www.cnblogs.com/Skyxu/p/14543806.html
Copyright © 2011-2022 走看看