步骤一、
Nuget Packages安装,使用程序包管理器控制台,安装命令:Install-Package Swashbuckle.AspNetCore -Pre
步骤二、
在Startup 文件中添加配置:
public void ConfigureServices(IServiceCollection services){ // Add framework services. services.AddMvc() .AddJsonOptions(options => options.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.DefaultContractResolver());//JSON首字母小写解决 services.AddSwaggerGen(options => { options.SwaggerDoc("v1", new Info { Version = "v1", Title = "MsSystem API" }); //Determine base path for the application. var basePath = PlatformServices.Default.Application.ApplicationBasePath; //Set the comments path for the swagger json and ui. var xmlPath = Path.Combine(basePath, "MsSystem.API.xml");//右键项目属性,生成xml配置文件,把名称输入即可 options.IncludeXmlComments(xmlPath); }); }
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory){ loggerFactory.AddConsole(Configuration.GetSection("Logging")); loggerFactory.AddDebug(); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Home/Error"); } app.UseMvc(); app.UseSwagger(); app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "MsSystem API V1"); }); }
直接访问地址 http://localhost:*/swagger/index.html