網站首頁 編程語言 正文
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
相關推薦
- 2022-08-13 SpringSecurity+JWT實現認證及授權詳細步驟
- 2024-02-29 UNI-APP中點擊事件多重響應問題的解決,list列表項item和列表項item中按鈕的點擊事件沖
- 2022-04-06 python中matplotlib的顏色以及形狀實例詳解_python
- 2022-07-08 一文詳解Python中生成器的原理與使用_python
- 2022-08-21 Caffe卷積神經網絡solver及其配置詳解_python
- 2022-10-09 ASP.NET泛型四之使用Lazy<T>實現延遲加載_實用技巧
- 2022-03-17 docker安裝rocketMQ和安裝過程中出現問題的解決_docker
- 2022-09-20 Python數字比較與類結構_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同步修改后的遠程分支