昂首阔步不显示真正的错误信息 [英] Swagger does not show real error message

查看:60
本文介绍了昂首阔步不显示真正的错误信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们使用的是.NET WebAPI框架,并且我们在Web api上使用了swagger进行注释和开箱即用的UI体验.到目前为止,它一直运行良好.但是,当我从WebAPI(Http 400)返回错误时,如下所示:

We are on .NET WebAPI framework and we are using swagger on top of web api for annotations and out of the box UI experience. So far, it just has been working great. However, when I return an error from WebAPI(Http 400) as following code:

return BadRequest("Some error");

但是,当我这样做时,似乎Swagger无法正确解析错误.基本上,显示响应0 .这肯定是错误的,因为当我转到 Chrome DevTools 时,我看到我的请求具有预期的HTTP 400响应.

However, when I do that, it seems like Swagger is not able to parse the error properly. It basically, show response 0. Which is wrong for sure because when I go to Chrome DevTools, I see that my request had a response of HTTP 400 as I expected.

如何配置Swagger,使其能够正确显示错误?还是应该以一些不同的方式抛出错误才能看到它?

How can I configure Swagger so that it is able to show the errors correctly? Or should I be throwing my errors a little bit differently to be able to see it in swagger?

推荐答案

您可以注释这样的操作

[ProducesResponseType(typeof(ResponseObjectTypeHere), 200)]
[ProducesResponseType(typeof(ErrorResponseObjectTypeHere), 400)]

如果您启用swagger读取和使用xml文档,也可以在xml文档中添加类似的内容

You can also add something like this in the xml documentation if you enable swagger to read and use xml documentation

/// <response code="200">Description</response>
/// <response code="400">Description of error</response>

我在.net核心Web api项目中用招摇晃的方式使用了这些内容,对于常规.net Web api项目也应相同.

I used those in the .net core web api project with swagger, should be the same for regular .net web api project as well.

这篇关于昂首阔步不显示真正的错误信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆