zoukankan      html  css  js  c++  java
  • .NET Core WEB API使用Swagger生成在线接口文档

    1项目引用Swashbuckle.AspNetCore程序集和Microsoft.Extensions.PlatformAbstractions程序集

      右击项目打开"管理NuGet程序包...",选择Swashbuckle.AspNetCore进行安装,如下图所示:

      选择选择Microsoft.Extensions.PlatformAbstractions进行安装,如下图所示:

    2.在工程中配置Swagger

      在Startup.cs类中的ConfigureServices中添加如下代码:

            // 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(options =>
                {
                    options.SwaggerDoc("v1", new Info
                    {
                        Version = "v1",
                        Title = "Swagger示例 API"
                    });
                    //Determine base path for the application.  
                    var basePath = PlatformServices.Default.Application.ApplicationBasePath;
                });
            }

    在Configure方法中添加如下代码:

            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引用
                app.UseSwagger();
                app.UseSwaggerUI(c =>
                {
                    c.SwaggerEndpoint("/swagger/v1/swagger.json", "Swagger示例API V1");
                });
            }

    3.Swagger在线接口文档效果

      工程运行后,在服务地址的端口号后面输入swagger/index.html,在线文档的效果就出来了,如下图所示:

    4.让方法和字段显示文字注释

      首先右击项目,选择“属性”,让项目输出注释xml文档,如下图所示:

      在ConfigureServices方法中添加如下代码:

            public void ConfigureServices(IServiceCollection services)
            {
                services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
                services.AddSwaggerGen(options =>
                {
                    options.SwaggerDoc("v1", new Info
                    {
                        Version = "v1",
                        Title = "Swagger示例 API"
                    });
                    //显示文档注释
                    var basePath = PlatformServices.Default.Application.ApplicationBasePath;
                    var xmlPath = Path.Combine(basePath, "SwaggerTest.xml");
                    options.IncludeXmlComments(xmlPath);
                });
            }

      给相应的接口方法和实体字段添加注释后,启动项目效果如下所示:

    5.把Swagger文档设置为项目的启始页

      打开工程的launchSettings.json配置文件,把"launchUrl": 的地址修改为"swagger/index.html",如下图所示:

  • 相关阅读:
    JS 利用数组拼接html字符串
    IE浏览器下读取客户端上传的文件大小
    PrintWriter out = response.getWriter() 输出中文乱码问题
    非常有用的Java程序片段
    sql之left join、right join、inner join的区别
    JAVA 数组常用技巧
    java 图片文件格式转换(多页tif转jpg 、jpg转tif)
    SQL Server 字段状态判断语句
    sql server 2008中id如何设为自增
    java基于xml配置的通用excel单表数据导入组件(五、Action处理类)
  • 原文地址:https://www.cnblogs.com/fengye310/p/10781040.html
Copyright © 2011-2022 走看看