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