網站首頁 編程語言 正文
1.添加Nuget
install-package Swashbuckle.AspNetCore -project XXX -version 6.4.0
2.添加靜態類擴展方法
2.1.生成項目xml:選中項目 / 右鍵 / 屬性 / 生成 / 輸出 / 選中xml文檔文件
2.2.system_v1:必須唯一不重復,且【options.SwaggerDoc("system_v1"】必須與【options.SwaggerEndpoint("/swagger/system_v1/】一致,不然會異?!綟ailed to load API definition; Fetch error: response status is 404 /swagger/system_v1/swagger.json】
/// <summary>
/// Swagger 靜態類
/// </summary>
public static class SwaggerExtend
{
/// <summary>
/// 添加服務: swagger
/// </summary>
/// <param name="services"></param>
/// <returns></returns>
public static void AddCustSwagger(this IServiceCollection services)
{
var version = "V1.0";
var apiName = "XXX系統";
services.AddSwaggerGen(options =>
{
options.ResolveConflictingActions(apiDescriptions => apiDescriptions.First());
options.SwaggerDoc("system_v1", new OpenApiInfo
{
Version = version,
Title = $"{apiName} API",
Description = $"{apiName} {version} 接口服務"
});
// 獲取應用程序所在目錄
var basePath = Path.GetDirectoryName(typeof(SwaggerExtend).Assembly.Location);
var xmlPath = Path.Combine(basePath, "ProjectName.xml");
// swagger界面默認只顯示 方法&字段 注釋,不顯示 控制器注釋
// 第二個參數為true, 則是controller的注釋
//options.IncludeXmlComments(xmlPath);
options.IncludeXmlComments(xmlPath, true);
});
}
/// <summary>
/// 添加中間件: swagger
/// </summary>
/// <param name="app"></param>
public static void UseCustSwagger(this IApplicationBuilder app)
{
app.UseSwagger();
app.UseSwaggerUI(options =>
{
options.SwaggerEndpoint("/swagger/system_v1/swagger.json", "系統API");
// 默認路徑為:/swagger/index.html
// 路由前綴 - 設置為空,可直接跳轉到swagger頁面:/index.html
// api測試可設置為空,部署時容易與前端路由沖突
options.RoutePrefix = string.Empty;
});
}
}
3.StartUp注冊服務,添加中間件
public void ConfigureServices(IServiceCollection services)
{
services.AddCustSwagger();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseCustSwagger();
}
原文鏈接:https://www.cnblogs.com/CRobot/archive/2022/09/21/16715909.html
相關推薦
- 2023-11-12 Docker中如何限制容器可用的 CPU
- 2022-11-17 React中實現插槽效果的方案詳解_React
- 2022-03-27 C語言中scanf與scnaf_s函數詳解_C 語言
- 2023-08-01 elementui全局給select option添加title屬性
- 2022-06-02 C語言的模板與泛型編程你了解嗎_C 語言
- 2022-03-19 好用的C++?string?Format“函數”介紹_C 語言
- 2023-04-27 c++中關于max_element()函數解讀_C 語言
- 2023-05-10 python中使用numpy包的向量矩陣相乘np.dot和np.matmul實現_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支