【官檔整理】ASP.NET Core WebAPI 安裝 Swagger 組件

官檔地址:

https://docs.microsoft.com/zh-cn/aspnet/core/tutorials/getting-started-with-swashbuckle?view=aspnetcore-2.1

個人整理:

1、開啓項目文檔:屬性-生成-XML文檔文件,debug和release都要配置,成功後.csproj文件中會出現

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
    <DocumentationFile>ProjectName.xml</DocumentationFile>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
    <DocumentationFile>ProjectName.xml</DocumentationFile>
  </PropertyGroup>

2、Nuget安裝 Swashbuckle.AspNetCore

3、Startup.ConfigureServices中添加

            services.AddSwaggerGen(options =>
            {
                options.SwaggerDoc("doc", new Info() { Title = "doc" });
                options.IncludeXmlComments(Path.Combine(PlatformServices.Default.Application.ApplicationBasePath, "ProjectName.xml"));
            });

4、Startup.Configure中添加

            app.UseSwagger();
            app.UseSwaggerUI(options => options.SwaggerEndpoint("/swagger/doc/swagger.json", null));

5、訪問地址:/swagger/index.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章