.Netcore Swagger - 解決外部庫導致的“Actions require an explicit HttpMethod binding for Swagger 2.0”


現象:

項目中導入Ocelot后,swagger頁面無法正常顯示,查看異常發現 Ocelot.Raft.RaftController 中的 Action 配置不完全,swagger掃描時不能正確生成 swagger.json

解決方法:

在掃描中隱藏Ocelot的controller,避免被swagger生成文檔

創建ApiExplorerIgnores

    public class ApiExplorerIgnores : IActionModelConvention
    {
        /// <summary>
        /// Ocelot自帶的Controller與swagger2.0沖突,在此排除掃描
        /// </summary>
        /// <param name="action"></param>
        public void Apply(ActionModel action)
        {
            //沖突的Ocelot.Raft.RaftController
            if (action.Controller.ControllerName.Equals("Raft"))
                action.ApiExplorer.IsVisible = false;
            //Ocelot.Cache.OutputCacheController
            if (action.Controller.ControllerName.Equals("OutputCache"))
                action.ApiExplorer.IsVisible = false;
        }
    }

 

setup.cs中添加

services.AddMvc(c => c.Conventions.Add(new ApiExplorerIgnores()));

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM