【官档整理】ASP.NET Core WebAPI 安装 Swagger(Swashbuckle) 组件

官档地址:

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 = "@title" });
                options.IncludeXmlComments("ProjectName.xml");
            });

4、Startup.Configure中添加

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

5、访问地址:/swagger/index.html

补充-如果还需要调试时启动,修改launchSettings.json

{
    "iisSettings": {
        "windowsAuthentication": false,
        "anonymousAuthentication": true,
        "iisExpress": {
            "applicationUrl": "http://localhost:5857/",
            "sslPort": 0
        }
    },
    "profiles": {
        "IIS Express": {
            "commandName": "IISExpress",
            "launchBrowser": true,
            "launchUrl": "swagger",
            "environmentVariables": {
                "ASPNETCORE_ENVIRONMENT": "Development"
            }
        },
        "ProjectName": {
            "commandName": "Project",
            "launchBrowser": true,
            "launchUrl": "swagger",
            "environmentVariables": {
                "ASPNETCORE_ENVIRONMENT": "Development"
            },
            "applicationUrl": "http://localhost:5858/"
        }
    }
}

猜你喜欢

转载自blog.csdn.net/fromfire2/article/details/81130525
今日推荐