.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()));