网络核心API:ProducesResponseType的用途 [英] Net Core API: Purpose of ProducesResponseType

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

问题描述

我想了解 ProducesResponseType的目的。

Microsoft将其定义为 a过滤器,指定操作返回的值和状态码的类型。

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/zh-CN/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

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

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