.net core 3.1 引入Swagger

第一步:引入包

  Swashbuckle.AspNetCore

  Microsoft.DotNet.PlatformAbstractions

第二步:设置项目属性

第三步:服务注册

        private readonly string apiName = "基础用户信息服务";

            #region swagger
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("V1", new OpenApiInfo
                {
                    Version = "V1",//版本号
                    Title = $"{apiName} 接口文档——dotnetcore 3.1",//编辑标题
                    Description = $"{apiName} HTTP API V1",//编辑描述
                    Contact = new OpenApiContact { Name = apiName, Email = "2334344234@163.com" },//编辑联系方式
                    License = new OpenApiLicense { Name = apiName }//编辑许可证
                });
                c.OrderActionsBy(o => o.RelativePath);

                var xmlPath = Path.Combine(Microsoft.DotNet.PlatformAbstractions.ApplicationEnvironment.ApplicationBasePath, "NetCore.SwaggerDemo.xml");// 配置接口文档文件路径
                c.IncludeXmlComments(xmlPath, true); // 把接口文档的路径配置进去。第二个参数表示的是是否开启包含对Controller的注释容纳
            });
            #endregion
第四步:添加中间件

            #region swagger
            app.UseSwagger();
            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint($"/swagger/V1/swagger.json", $"{apiName} V1");
                c.RoutePrefix = "";
            });
            #endregion
第五步:修改launchSettings.json文件,让网站一启动的时候,自动进入swagger界面

把这俩都改成空字符串即可。

第六步:在控制器和方法上写上注释,然后运行网站,查看效果
原文地址:https://www.cnblogs.com/gygtech/p/14243736.html