zoukankan      html  css  js  c++  java
  • .Net Core 使用swagger UI

    swagger UI 

    第一步:首先添加包Swashbuckle.AspNetCore (可通过微软添加包命令Install-Package 包名进行添加,也可以通过管理NuGet程序包进行添加)

    第二步:修改launchUrl为swagger,即程序启动后进入swagger UI风格页面也可以说Rest风格。

    {
      "$schema": "http://json.schemastore.org/launchsettings.json",
      "iisSettings": {
        "windowsAuthentication": false, 
        "anonymousAuthentication": true, 
        "iisExpress": {
          "applicationUrl": "http://localhost:55360",
          "sslPort": 44347
        }
      },
      "profiles": {
        "IIS Express": {
          "commandName": "IISExpress",
          "launchBrowser": true,
          "launchUrl": "swagger",
          "environmentVariables": {
            "ASPNETCORE_ENVIRONMENT": "Development"
          }
        },
        "WebApi": {
          "commandName": "Project",
          "launchBrowser": true,
          "launchUrl": "swagger",
          "applicationUrl": "https://localhost:5001;http://localhost:5000",
          "environmentVariables": {
            "ASPNETCORE_ENVIRONMENT": "Development"
          }
        }
      }
    }
    launchSettings.json

    第三步:在Startup.cs中的方法ConfigureServices方法中添加swagger 相关代码。

       public voidConfigureServices(IServiceCollection services)
            {
             
    
                services.AddMvc();
                #region  添加SwaggerUI
    
                services.AddSwaggerGen(options =>
                {
                    options.SwaggerDoc("v1", new Info
                    {
                        Title = "dapper API接口文档",
                        Version = "v1",
                        Description = "RESTful API for Dapper",
                        TermsOfService = "Z Terms Of Service",
                        Contact = new Contact { Name = "zhang", Email = "904086892@qq.com", Url = "" }
                    });
                });
                #endregion
      }
    ConfigureServices方法

    第四步:在Startup.cs中的方法Configure方法中添加swagger 相关代码。

     public void Configure(IApplicationBuilder app, IHostingEnvironment env)
            {            
                if (env.IsDevelopment())
                    app.UseDeveloperExceptionPage();
                else
                    app.UseHsts();
    
                #region 使用SwaggerUI
    
                app.UseSwagger();
                app.UseSwaggerUI(options =>
                {
                    options.SwaggerEndpoint("/swagger/v1/swagger.json", "Dapper API V1");
                });
    
                #endregion
                app.UseHttpsRedirection();
    
                app.UseMvc();
            }                    
    Configure方法

    最后运行项目,可在该页面下进行使用接口。效果如下:

  • 相关阅读:
    BZOJ3171: [Tjoi2013]循环格
    Luogu P1850 换教室(期望dp)
    Luogu P3825 [NOI2017]游戏(2-SAT)
    Luogu P3007 [USACO11JAN]大陆议会The Continental Cowngress
    Luogu P2272 [ZJOI2007]最大半连通子图(Tarjan+dp)
    Luogu P3209 [HNOI2010]平面图判定(2-SAT)
    Luogu P4171 [JSOI2010]满汉全席(2-SAT)
    Luogu P4782 【模板】2-SAT 问题(2-SAT)
    Luogu P2845 [USACO15DEC]Switching on the Lights 开关灯(bfs)
    Luogu P4933 大师(dp)
  • 原文地址:https://www.cnblogs.com/muyeh/p/9788475.html
Copyright © 2011-2022 走看看