.Netcore Swagger - 解决外部库导致的“Actions require an explicit HttpMethod binding for Swagger 2.0”

时间:2019-11-27
本文章向大家介绍.Netcore Swagger - 解决外部库导致的“Actions require an explicit HttpMethod binding for Swagger 2.0”,主要包括.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()));

原文地址:https://www.cnblogs.com/Ecol/p/11943753.html