• 四、ABP 学习系列


    一、再XX.Web项目中用Nuget安装Swashbuckle.AspNetCore.SwaggerGen和Swashbuckle.AspNetCore.SwaggerUI

    二、在Startup.cs中添加如下代码

    public class Startup
        {
            public IServiceProvider ConfigureServices(IServiceCollection services)
            {
              
                services.AddMvc(options =>
                {
                    options.Filters.Add(new AutoValidateAntiforgeryTokenAttribute());
                });
    
    
                //// Configure CORS for angular2 UI
                //services.AddCors(
                //    options => options.AddPolicy(
                //        _defaultCorsPolicyName,
                //        builder => builder
                //            .WithOrigins(
                //                // App:CorsOrigins in appsettings.json can contain more than one address separated by comma.
                //                _appConfiguration["App:CorsOrigins"]
                //                    .Split(",", StringSplitOptions.RemoveEmptyEntries)
                //                    .Select(o => o.RemovePostFix("/"))
                //                    .ToArray()
                //            )
                //            .AllowAnyHeader()
                //            .AllowAnyMethod()
                //    )
                //);
    
                // Swagger - Enable this line and the related lines in Configure method to enable swagger UI
                services.AddSwaggerGen(options =>
                {
                    options.SwaggerDoc("v1", new Info { Title = "M API", Version = "v1" });
                    options.DescribeAllEnumsAsStrings();                                        //Enum转字符串
                    options.DescribeAllParametersInCamelCase();                                 //开启驼峰规则
                    options.DocInclusionPredicate((docName, description) => true);              //显示备注
                    options.IncludeXmlComments(GetXmlCommentsPath("XX.Web"));                  // 注意:此处替换成所生成的XML documentation的文件名。
                    options.IncludeXmlComments(GetXmlCommentsPath("XX.Application"));          // 注意:此处替换成所生成的XML documentation的文件名。
                    // Define the BearerAuth scheme that's in use
                    options.AddSecurityDefinition("bearerAuth", new ApiKeyScheme()
                    {
                        Description = "JWT Authorization header using the Bearer scheme. Example: "Authorization: Bearer {token}"",
                        Name = "Authorization",
                        In = "header",
                        Type = "apiKey"
                    });
                    // Assign scope requirements to operations based on AuthorizeAttribute
                    options.OperationFilter<SecurityRequirementsOperationFilter>();
                });
    
            }
    
            public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
            {
    
                app.UseMvc(routes =>
                {
                    routes.MapRoute(
                        name: "default",
                        template: "{controller=Home}/{action=Index}/{id?}");
                });
    
    
                // Enable middleware to serve generated Swagger as a JSON endpoint
                app.UseSwagger();
                // Enable middleware to serve swagger-ui assets (HTML, JS, CSS etc.)
                app.UseSwaggerUI(options =>
                {
                    options.InjectOnCompleteJavaScript("/swagger/ui/abp.js");
                    options.InjectOnCompleteJavaScript("/swagger/ui/on-complete.js");
                    options.SwaggerEndpoint("/swagger/v1/swagger.json", "M API V1");
                }); // URL: /swagger
            }
    
            /// <summary>
            /// 获取帮助文件路径
            /// </summary>
            /// <param name="name">文件名</param>
            /// <returns></returns>
            protected string GetXmlCommentsPath(string name)
            {
                return string.Format(@"{0}{1}.XML", AppDomain.CurrentDomain.BaseDirectory, name);
            }
        }
  • 相关阅读:
    Big Number
    Who will be punished
    find your present (2)
    Being a Good Boy in Spring Festival
    day4__列表的初识(列表的创建、增删改查、元组、range)
    day3、基础___(基础数字类型、字符串索引与切片、str常用操作方法)
    day2、基础__(while循环、格式化输出、运算符、初始编码)
    day1: 基础 __ (变量、常量、注释、数据类型、input、 if)
    十八、FTP配置
    十七、交换机配置管理IP和telnet登陆设置
  • 原文地址:https://www.cnblogs.com/chuancheng/p/8196624.html
Copyright © 2020-2023  润新知