Net Core API:ProducesResponseType 的目的 [英] Net Core API: Purpose of ProducesResponseType

查看:60
本文介绍了Net Core API:ProducesResponseType 的目的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解ProducesResponseType的用途.

Microsoft 将定义为一个过滤器,用于指定操作返回的值和状态代码的类型.

Microsoft defines as a filter that specifies the type of the value and status code returned by the action.

所以我很好奇如果

  • 某人没有放置 ProductResponseType?
  • 系统如何处于劣势,或者是否有负面后果?
  • Microsoft API 不是已经自动知道返回的状态代码的类型/值吗?
[ProducesResponseType(typeof(DepartmentDto), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]

来自 Microsoft 的文档:ProducesResponseTypeAttribute 类

Documentation from the Microsoft: ProducesResponseTypeAttribute Class

推荐答案

我认为它可以为非成功 (200) 返回代码派上用场.假设其中一个失败状态代码返回一个描述问题的模型,您可以指定这种情况下的状态代码产生与成功案例不同的东西.您可以阅读更多相关信息并在此处找到示例:https://docs.microsoft.com/en-us/aspnet/core/web-api/action-return-types?view=aspnetcore-2.2

I think it can come handy for non-success (200) return codes. Say if one of the failure status codes returns a model that describes the problem, you can specify that the status code in that case produces something different than the success case. You can read more about that and find examples here: https://docs.microsoft.com/en-us/aspnet/core/web-api/action-return-types?view=aspnetcore-2.2

这篇关于Net Core API:ProducesResponseType 的目的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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