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

现象:json

项目中导入Ocelot后,swagger页面没法正常显示,查看异常发现 Ocelot.Raft.RaftController 中的 Action 配置不彻底,swagger扫描时不能正确生成 swagger.json

spa

解决方法:code

在扫描中隐藏Ocelot的controller,避免被swagger生成文档blog

建立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中添加io

services.AddMvc(c => c.Conventions.Add(new ApiExplorerIgnores()));
相关文章
相关标签/搜索