Search code examples
c#.netasp.net-core.net-core

Net Core API: Purpose of ProducesResponseType


I want to understand the purpose of ProducesResponseType.

Microsoft defines as:

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

So I am curious what are consequences if

  • a person does not place ProductResponseType?
  • How is system at a disadvantage, or is there negative consequence?
  • Doesn't Microsoft API already automatically inherently know the type/value of status code returned?
[ProducesResponseType(typeof(DepartmentDto), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]

Documentation from the Microsoft: ProducesResponseTypeAttribute Class


Solution

  • 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://learn.microsoft.com/en-us/aspnet/core/web-api/action-return-types?view=aspnetcore-2.2