Web Api - Swagger 文档错误 500

Web Api - Swagger documentation error 500

当我访问 swagger url: http://localhost:28483/swagger/ui/index 时,它会生成此错误:

500 : undefined http://localhost:28483/swagger/docs/v1

有什么想法吗?

更新: 在 firebug 中查看此详细错误:

Not supported by Swagger 2.0: Multiple operations
 with path 'api/BimModel' and method 'GET'. See the config setting - \"ResolveConflictingActions\" for
 a potential workaround

在控制器中,它有两个不同的 GET 操作,这是 Swagger 不允许的。 我建议每个控制器只有一个 GET 操作或 modify the router in WebApiConfig

您是否尝试过在 swagger 配置中启用此功能?

c.ResolveConflictingActions(apiDescriptions => apiDescriptions.First());

Swagger 可能会将两个操作视为一个操作(就像在这种常见情况下一样)...

GET api/Products
GET api/Products/{id}

看来您可以使用 attribute routing 来解决这个问题,并在您的操作上方使用这些属性,这样 swagger 会分别识别它们。

[Route("api/Products")]

[Route("api/Products/{id:guid}")]

我在将属性路由与默认路由混合使用时遇到了同样的问题。 当我删除默认路由时,问题就消失了。缺点是,没有定义默认路由,我不得不为我所有的控制器添加属性路由。

所以我从我的 WebApiConfig 中删除了:

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

并向我的控制器添加了属性路由:

[Route("Session")] // Added this attribute
public async Task<IHttpActionResult> Get()
...    
[Route("Session/{id}")] // Added this attribute
public async Task<IHttpActionResult> Get(int id)

实际上我在我的控制器上使用 [RoutePrefix("Session")] 并在我的方法上使用 [Route("")],但结果应该是一样的。

由于属性路由语句和方法签名之间的参数名称不匹配,我收到此错误。

[HttpGet("{id}")]
public IActionResult Get(string deviceNumber){
...

将“{id}”更改为“{deviceNumber}”后修复了错误。

config.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{action}/{id}",
    defaults: new { id = RouteParameter.Optional }
);