Add `host`, `basePath` and `schemes` to swagger.json using Swashbuckle Aspnetcore(使用 Swashbuckle Aspnetcore 将 `host`、`basePath` 和 `schemes` 添加到 swagger.json)
问题描述
我正在使用官方文档逐步方法来配置 Swagger UI 并在我的 ASP.NET 核心 API 应用程序中生成 Swagger JSON 文件.
I am using official doc step by step method to configure Swagger UI and generate Swagger JSON file in my ASP.NET core API application.
开始使用 Swashbuckle 和 ASP.NET Core
如果我查看生成的 swagger.json 文件 - 它缺少三个重要属性 host、basePath 和 schemes
If I look at my generated swagger.json file - it is missing three important properties host, basePath and schemes
请帮助我了解我可以添加哪些代码,以便生成的 swagger.json 将具有以下提到的属性/值.
Please help me understand what piece of code can I add so the swagger.json that gets generated will have following mentioned properties/values.
这是一个理想的 swagger.json - 如果我遵循文档,请注意缺少的 host、basePath 和 schemes 值我的应用程序中的代码
Here is an ideal swagger.json - give attention to the host, basePath and schemes values which are missing if I follow the documentation code in my application
{
"swagger": "2.0",
"info": {
"version": "v1",
"title": "Demo API Title"
},
"host": "some-url-that-is-hosted-on-azure.azurewebsites.net",
"basePath": "/api",
"schemes": ["https"],
"paths": {
"/Account/Test": {
"post": {
"tags": [
"Admin"
],
"summary": "Account test method - POST",
"operationId": "AccountTest",
"consumes": [],
"produces": [
"text/plain",
"application/json",
"text/json"
],
"parameters": [],
"responses": {
"200": {
"description": "Success",
"schema": {
"type": "boolean"
}
}
}
}
}
},
"definitions": {
"NumberSearchResult": {
"type": "object",
"properties": {
"number": {
"type": "string"
},
"location": {
"type": "string"
}
}
}
},
"securityDefinitions": {
"Bearer": {
"name": "Authorization",
"in": "header",
"type": "apiKey",
"description": "Authorization. Example: "Authorization: Bearer {token}""
}
},
"security": [
{
"Bearer": []
}
]
}
推荐答案
您可以实现并注册您自己的 IDocumentFilter 并在那里设置所需的值.
You can implement and register your own IDocumentFilter and set the desired values there.
public class MyDocumentFilter : IDocumentFilter
{
public void Apply(SwaggerDocument swaggerDoc, SchemaRegistry schemaRegistry, IApiExplorer apiExplorer)
{
swaggerDoc.Host = "some-url-that-is-hosted-on-azure.azurewebsites.net";
swaggerDoc.BasePath = "/api";
swaggerDoc.Schemes = new List<string> { "https" };
}
}
然后通过
services.AddSwaggerGen(options =>
{
options.DocumentFilter<MyDocumentFilter>();
});
这篇关于使用 Swashbuckle Aspnetcore 将 `host`、`basePath` 和 `schemes` 添加到 swagger.json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 Swashbuckle Aspnetcore 将 `host`、`basePath` 和 `schemes` 添加到 swagger.json
基础教程推荐
- 获取C#保存对话框的文件路径 2022-01-01
- Mono https webrequest 失败并显示“身份验证或解密失败" 2022-01-01
- 更新 Visual Studio 中的 DataSet 结构以匹配新的 SQL 数据库结构 2022-01-01
- 如果条件可以为空 2022-01-01
- C# 9 新特性——record的相关总结 2023-04-03
- 将数据集转换为列表 2022-01-01
- 从 C# 控制相机设备 2022-01-01
- SonarQube C# 分析失败“不是指针的有效行偏移" 2022-01-01
- 在 VB6 或经典 ASP 中使用 .NET 2022-01-01
- 重新排序 WPF TabControl 中的选项卡 2022-01-01
