VSCode下WebApi+Swagger-ui插件配置

安装包 swashbuckle.aspnetcore

$ dotnet add package swashbuckle.aspnetcore

修改 startup.cs

  • 添加引用

    using Swashbuckle.AspNetCore.Swagger;
    using Microsoft.Extensions.PlatformAbstractions;
    using System.IO;
    
  • 在 ConfigureServices 方法内添加

services.AddSwaggerGen(c =>
  {
      c.SwaggerDoc("v1", new Info
      {
          Version = "v1",
          Title = "Demo Api"
      });
      var basePath = PlatformServices.Default.Application.ApplicationBasePath;
      var xmlPath = Path.Combine(basePath, "CoreApi.xml"); //CoreApi.xml 自己定义名称
      c.IncludeXmlComments(xmlPath);
  }
);
  • 在 Configure 方法内添加
    app.UseSwagger();
    app.UseSwaggerUI(c => {
      c.SwaggerEndpoint("/swagger/v1/swagger.json", "DemoApi");
    });
    

修改工程文件 xxxx.csproj 添加

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DocumentationFile>bin\Debug\netcoreapp2.0\CoreApi.xml</DocumentationFile>
</PropertyGroup>