首页 > 解决方案 > 为什么 Swagger/Swashbuckle 会破坏我的路线?

问题描述

我有一个控制器,上面有以下方法:

[System.Web.Mvc.HttpGet]
[System.Web.Mvc.Route("api/v1.0/foo/{id}")]
[ResponseType(typeof(MyObject))]
public IHttpActionResult Get(int id)

[System.Web.Mvc.HttpGet]
[System.Web.Mvc.Route("api/v1.0/foo/{id}/address")]
[ResponseType(typeof(MyObject))]
public IHttpActionResult Get(int id)

如果我删除第二种方法,控制器在 Swagger 中呈现得很好。但是,如果我包含它,路线将呈现如下:

GET /api/Foo
GET /api/Foo/{id}

发生了什么事,我该如何解决?

标签: c#.netswaggerswagger-uiswashbuckle

解决方案


原来这是由我使用的属性引起的。(复制粘贴再次罢工。)

这些都不正确:

[System.Web.Mvc.HttpGet]
[System.Web.Mvc.Route("api/v1.0/foo/{id}")]

相反,这些正确的:

[System.Web.Http.HttpGet]
[System.Web.Http.Route("api/v1.0/foo/{id}")]

推荐阅读