码迷,mamicode.com
首页 > 其他好文 > 详细

四、ABP 学习系列 - 配置Swagger

时间:2018-01-05 01:26:26      阅读:1245      评论:0      收藏:0      [点我收藏+]

标签:netcore   init   log   line   inf   文件名   pre   string   default   

一、再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

标签:netcore   init   log   line   inf   文件名   pre   string   default   

原文地址:https://www.cnblogs.com/chuancheng/p/8196624.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!