ABP 学习系列 - 配置Swagger

Posted 昨日微风

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ABP 学习系列 - 配置Swagger相关的知识,希望对你有一定的参考价值。

一、再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);
        }
    }

 

以上是关于ABP 学习系列 - 配置Swagger的主要内容,如果未能解决你的问题,请参考以下文章

Abp Vnext手动搭建简单项目系列5

ABP框架系列学习启动配置

ABP架构学习系列

ABP微服务系列学习-对接Apollo配置中心

abp学习目录

Abp中SwaggerUI的接口说明文档配置