.net core 安装Swagger

Install-Package Swashbuckle -Pre

1.Startup

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
    // Add framework services.
    services.AddMvc();

    //******************* swagger start ***********************
    services.AddSwaggerGen(c =>
    {
        c.SingleApiVersion(new Info
        {
            Version = "v1",     // 这个属性必须要填,否则会引发一个异常
            Title = "Feature List",
            Description = "特征"
        });
    });

    services.ConfigureSwaggerGen(c =>
    {
        // 配置生成的 xml 注释文档路径
        c.IncludeXmlComments(GetXmlCommentsPath());
    });

    //******************* swagger end ***********************
}

private string GetXmlCommentsPath()
{
    var app = PlatformServices.Default.Application;
    return Path.Combine(app.ApplicationBasePath, Path.ChangeExtension(app.ApplicationName, "xml"));
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    loggerFactory.AddConsole(Configuration.GetSection("Logging"));
    loggerFactory.AddDebug();

    app.UseMvc();

    //******************* swagger start ***********************
    app.UseSwagger();
    app.UseSwaggerUi("swagger/ui/index");
    //******************* swagger end ***********************
}

如上:
//******************* swagger start ***********************
//******************* swagger end ***********************

2.launchSettings.json

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:40675/",
      "sslPort": 0
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "launchUrl": "swagger/ui/index",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "SigmalHex.WebApi": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "swagger/ui/index",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "applicationUrl": "http://localhost:40676"
    }
  }
}

如:
"launchUrl": "swagger/ui/index",

转载于:https://www.cnblogs.com/pengzhen/p/6909857.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值