首页 文章

使用Swashbuckle更改swagger JSON的位置

提问于
浏览
3

我正在尝试配置Swashbuckle,因此可以使用URL /swagger.json访问生成的JSON文件 .

我已经操纵了许多设置但却无法使其工作 . 这里有些例子:

// This works!  JSON file is located at http://{root}/swagger/docs/v1
this.EnableSwagger(c =>
{
    c.RootUrl(x => baseUrl);
    c.SingleApiVersion("v1", title);
}).EnableSwaggerUi();

This works!  JSON file is located at http://{root}/swagger/docs/swagger
this.EnableSwagger(c =>
{
    c.RootUrl(x => baseUrl);
    c.SingleApiVersion("swagger", title);
}).EnableSwaggerUi();

// This does not work.  JSON file is located at http://{root}/swagger
this.EnableSwagger("{apiVersion}", c =>
{
    c.RootUrl(x => baseUrl);
    c.SingleApiVersion("swagger", title);
}).EnableSwaggerUi();

// This does not work.  JSON file is located at http://{root}/foo/swagger
this.EnableSwagger("foo/{apiVersion}", c =>
{
    c.RootUrl(x => baseUrl);
    c.SingleApiVersion("swagger", title);
}).EnableSwaggerUi();

我们如何配置Swashbuckle,以便将文件命名为“swagger.json”,并从“/ swagger / docs”的不同路径访问 - 最好是应用程序的根目录?

1 回答

  • 2

    万一有人还在寻找:

    this approach为我工作:

    app.UseSwagger()更改:app.UseSwagger(c =>
    {
    c.RouteTemplate =“SampleApi / swagger /
    /swagger.json”;
    });
    app.UseSwaggerUI()更改:app.UseSwaggerUI(c =>
    {
    c.SwaggerEndpoint(“/ SampleApi / swagger / v1 / swagger.json”,“Sample API”);
    c.RoutePrefix =“SampleApi / swagger”;
    });

相关问题