mac*_*acm 6 asp.net-web-api swagger asp.net-core
很确定我错过了一些明显但没有看到的东西。
如何使用更新后的 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();
}
Run Code Online (Sandbox Code Playgroud)
我尝试了几种不同的变体,但似乎都不是窍门。我真的不想重写 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"
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
您需要配置PhysicalFileProvider并将您的swagger.json放入wwwroot或PhysicalFileProvider可访问的任何位置。之后您可以使用它访问它IFileProvider
参考:https://www.c-sharpcorner.com/article/file-providers-in-asp-net-core/
编辑如果您只是添加app.UseStaticFiles();到您的 StartUp 中,您可以轻松访问 wwwroot。
完全不同的方法
您还可以考虑使用控制器/操作来提供文件
public IActionResult GetSwaggerDoc()
{
var file = Path.Combine(Directory.GetCurrentDirectory(),
"MyStaticFiles", "swagger.json");
return PhysicalFile(file, "application/json");
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4464 次 |
| 最近记录: |