Swagger使用自定义的swagger.json文件aspnet核心

时间:2019-01-03 22:24:13

标签: asp.net-web-api asp.net-core swagger

很确定我缺少明显明显但看不到的东西。

如何使用更新后的swagger.json文件?

我获取了样板swagger / v1 / swagger.json代码并将其粘贴到editor.swagger.io系统中。然后,我更新了说明等,在模型中添加了示例,然后将内容另存为swagger.json。 将文件移到我的api应用程序的根目录中,将文件设置为始终复制。

 public void ConfigureServices(IServiceCollection services)
    {...
        services.AddSwaggerGen(c => { c.SwaggerDoc("V1", new Info {Title = "Decrypto", Version = "0.0"}); });

    }


 public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
      ...
        app.UseSwagger();
      //--the default works fine
     //  app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/V1/swagger.json", "Decrypto v1"); });
        app.UseSwaggerUI(c => { c.SwaggerEndpoint("swagger.json", "Decrypto v1"); });

        app.UseMvc();
    }

我尝试了几种不同的变体,但似乎没有窍门。我真的不想重写SwaggerDoc中的工作,因为对我而言,将文档放入运行时似乎很脏。

我要使用的自定义swagger.json文件如下:

        {
      "swagger": "2.0",
      "info": {
        "version": "0.0",
        "title": "My Title"
      },
      "paths": {
        "/api/Decryption": {
          "post": {
            "tags": [
              "API for taking encrypted values and getting the decrypted values back"
            ],
            "summary": "",
            "description": "",
            "operationId": "Post",
            "consumes": [
              "application/json-patch+json",
              "application/json",
              "text/json",
              "application/*+json"
            ],
            "produces": [
              "text/plain",
              "application/json",
              "text/json"
            ],
            "parameters": [
              {
                "name": "units",
                "in": "body",
                "required": true,
                "schema": {
                  "uniqueItems": false,
                  "type": "array",
                  "items": {
                    "$ref": "#/definitions/EncryptedUnit"
                  }
                }
              }
            ],
            "responses": {
              "200": {
                "description": "Success",
                "schema": {
                  "uniqueItems": false,
                  "type": "array",
                  "items": {
                    "$ref": "#/definitions/DecryptedUnit"
                  }
                }
              }
            }
          }
        }
      },
      "definitions": {
        "EncryptedUnit": {
          "type": "object",
          "properties": {
            "value": {
              "type": "string",
              "example": "7OjLFw=="
            },
            "initializeVector": {
              "type": "string",
              "example": "5YVg="
            },
            "cipherText": {
              "type": "string",
              "example": "596F5AA48A882"
            }
          }
        },
        "DecryptedUnit": {
          "type": "object",
          "properties": {
            "encrypted": {
              "type": "string",
              "example": "7OjLV="
            },
            "decrypted": {
              "type": "string",
              "example": "555-55-5555"
            }
          }
        }
      }
    }

2 个答案:

答案 0 :(得分:1)

您需要配置PhysicalFileProvider并将swagger.json放入wwwroot或PhysicalFileProvider可访问的任何位置。之后,您可以使用IFileProvider

访问它

参考:https://www.c-sharpcorner.com/article/file-providers-in-asp-net-core/


编辑:如果您仅将app.UseStaticFiles();添加到启动中,则可以轻松访问wwwroot。

Reference


完全不同的方法

您还可以考虑使用Controller / Action提供文件

public IActionResult GetSwaggerDoc()
{
    var file = Path.Combine(Directory.GetCurrentDirectory(), 
                            "MyStaticFiles", "swagger.json");

    return PhysicalFile(file, "application/json");
}

答案 1 :(得分:0)

.NET Core 2.2可以将物理文件服务器存储为如下所示的url资源。
但是,如果您使用自定义swagger json,则您的api是固定的,除非您每次都对其进行更改。

    public void Configure(IApplicationBuilder app, IHostingEnvironment env,
        ILoggerFactory loggerFactory)
    {   
        ...
        app.UseStaticFiles(new StaticFileOptions
        {
            FileProvider = new PhysicalFileProvider(
                Path.Combine(Directory.GetCurrentDirectory(),
                    "swagger/v1/swagger.json")),
            RequestPath = "swagger/v1/swagger.json"
        });
    }