.net core 引入SwaggerUI教程

Swagger

Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。方便前后端接口对接。

1.打开NuGet程序包,搜索“Swashbuckle.AspNetCore”安装。
注意:NETCore3.0版本需要使用Swashbuckle.AspNetCore 5.0以上的版本。自己使用的是.NET Core 2.2。

2.搜索“Swashbuckle.AspNetCore.Swagger ”安装。描述:基于.net core 的api显示的中间件

3.修改 Startup.cs 文件

 

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Swashbuckle.AspNetCore.Swagger;
using Swashbuckle.AspNetCore.SwaggerUI;
using System.IO;

namespace CarteVitalServerAPI
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new Info
                {
                    Title = "CarteVital文档",
                    Version = "1.0.0.3",
                    Description = "test文档",
                    TermsOfService = "None",//服务条款
                    Contact = new Contact {  Name=""}
                }) ;
                //var basePath = Path.GetDirectoryName(typeof(Program).Assembly.Location);//获取应用程序所在目录(绝对,不受工作目录影响,建议采用此方法获取路径)
                //var xmlPath = Path.Combine(basePath, "CarteVitalServerAPI.xml");
                //c.IncludeXmlComments(xmlPath);
            });
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }
            app.UseHttpsRedirection();
            app.UseMvc();

            //启用中间件服务生成Swagger作为JSON终结点
            app.UseSwagger(c => { c.RouteTemplate = "swagger/{documentName}/swagger.json"; });
            //启用中间件服务对swagger-ui,指定Swagger JSON终结点
            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("v1/swagger.json", "ProjectFlowMangement10WebApi API"); c.RoutePrefix = "swagger";
                c.DocExpansion(DocExpansion.None);
                c.DefaultModelsExpandDepth(-1);
            });
        }
    }
}
但是代码方法和参数并无关键注释说明如需显示注释说明。

 

 注:如需添加注释说明 ,需要将上面红色注释代码打开。

并按照以下步骤对项目进行修改。

 

 

 

 结果如图:

 

 

 

 

 

posted @ 2019-11-18 14:10  i丶星空  阅读(1235)  评论(0编辑  收藏  举报