加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

ASP.NET Web Api中的Swashbuckle被嵌套控制器搞糊涂了

发布时间:2020-12-16 09:20:01 所属栏目:asp.Net 来源:网络整理
导读:在我的ASP.NET Web Api(ASP.NET 4 MVC 5)应用程序中,我有两条路由配置如下: // Default route. Comes "out of the box" with Web Api. // e.g. api/users/123 config.Routes.MapHttpRoute( name: "DefaultApi",routeTemplate: "api/{controller}/{id}",defa
在我的ASP.NET Web Api(ASP.NET 4 MVC 5)应用程序中,我有两条路由配置如下:

// Default route. Comes "out of the box" with Web Api.
        // e.g. api/users/123
        config.Routes.MapHttpRoute(
            name: "DefaultApi",routeTemplate: "api/{controller}/{id}",defaults: new { id = RouteParameter.Optional }
        );

        // A route for child controllers. See for example the LockController 
        // that is "nested" under the "UsersController".
        // e.g. api/users/123/lock
        config.Routes.MapHttpRoute(
            name: "ChildApi",routeTemplate: "api/{parentController}/{parentId}/{controller}/{id}",defaults: new { id = RouteParameter.Optional }
        );

这些实际上工作得很好,因为我有一个匹配第一个路由的“顶级”UsersController和一个与第二个路由匹配的“子”LockController(在Users文件夹中).

现在我已经为我的应用程序添加了Swashbuckle 5(Swagger for .net),Swagger很困惑地显示了我实现的每个方法的两个路由.例如:

enter image description here

因此Swagger将LockController识别为顶级控制器和子控制器.

我如何告诉Swagger特定控制器是顶级还是孩子?

另外,我可以让swagger认识到锁定资源的路径,例如,/ api / users / {id} / lock而不是/ api / {parentController} / {parentId} / lock正如Swagger当前显示的那样?

解决方法

我认为问题与继承层次结构无关. Swashbuckle为每个路由映射生成路由.为了防止Swashbuckle生成重复的路由,一个解决方案可能是添加控制器约束到路由.我希望这会有所帮助.

config.Routes.MapHttpRoute(
            name: "DefaultApi",defaults: new { id = RouteParameter.Optional },constraints: new { controller = "Users" }
        );

          config.Routes.MapHttpRoute(
            name: "ChildApi",constraints: new { controller = "Lock" }

        );

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读