VS2017 + NetCore2.2.0 + WebApi project integration and pit encountered SwaggerUI

1, a new WebApi project, do not say here.

2, open the project nuget management console, in https://www.nuget.org/ search swagger package: Swashbuckle.AspNetCore, as shown in

 

3. Select the stable version and install command to copy the project nuget Package Manager Console to perform, and then wait for the installation

 

4, after the installation is complete, open Startup.cs file, reference the namespace

using Swashbuckle.AspNetCore.Swagger;

Add the following code process ConfigureServices

 public void ConfigureServices(IServiceCollection services)
        {
            ...

            services.AddSwaggerGen(options =>
            {
                options.SwaggerDoc("v1", new Info
                {
                    Version = "v1",
                    Title = "MsSystem API",
                    Description = "A simple example ASP.NET Core Web API"
                });
            });
        ... }

在Configure方法中添加代码

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseMvc();
            app.UseSwagger();
            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "MsSystem API V1");
            });
        }

配置完成;

5、发布一遍该webapi项目到IIS上:

这里注意一个坑,坑了我半天,不然不能访问到swagger.json文件与swaggerui页面出不来,必须将发布目录的文件夹权限打开(允许写入,读取)

6、在浏览器访问swagger.json文件的位置

如:http://localhost:8687/swagger/v1/swagger.json

7、访问swaggerui的接口调试页面

如:http://localhost:8687/swagger/index.html

 

感谢别人的经验,更多配置可以参考:https://yq.aliyun.com/articles/608901

 

Guess you like

Origin www.cnblogs.com/luckypc/p/10981197.html