Swagger使用自定义的swagger.json文件aspnet核心 [英] Swagger use a custom swagger.json file aspnet core

查看:147
本文介绍了Swagger使用自定义的swagger.json文件aspnet核心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Pretty sure I am missing something clearly obvious but not seeing it.

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

How can I use my updated swagger.json file?

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

I took my boilerplate swagger/v1/swagger.json code and pasted it into the editor.swagger.io system. I then updated the descriptions etc, added examples to my models and then saved the contents as swagger.json. Moved the file into the root of my api application, set the file to copy always.

 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中的工作,因为对我而言,将文档放入运行时似乎很脏.

I have tried a few different variation but none seem to be the trick. I don't really want to rewrite the work in SwaggerDoc as it seems dirty to me put documentation in the runtime.

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

the custom swagger.json file I want to use looks like this:

        {
      "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"
            }
          }
        }
      }
    }

推荐答案

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

you need to configure PhysicalFileProvider and put your swagger.json into wwwroot or anywhere accessible by PhysicalFileProvider. After that you can access it using IFileProvider

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

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

Edit If you just add app.UseStaticFiles(); into your StartUp, you can access wwwroot without hastle.

参考

完全不同的方法

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

you may also consider to serve your file using Controller/Action

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

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

这篇关于Swagger使用自定义的swagger.json文件aspnet核心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆