

[ProducesResponseType(StatusCodes.Status400BadRequest)]
We shall specify the possible response type of each method in our Controller which will generate related documentation in the Swagger UI. But it will be a tedious process to analyze and add all the methods in our API.
For this, ASP.NET Core 2.2 shipped an API Analyzer that will analyze the API actions and provide code fix that can be added in the code.
We shall the package “Microsoft.AspNetCore.Mvc.Api.Analyzers” from Nuget Package manager


[assembly: ApiConventionType(typeof(DefaultApiConventions))]
namespace APITest.Controllers
{
....
}
These DefaultApiConventions can also be extended and defined separately based on our needs.
DefaultApiConventions covers some basic commonly used convention types in APIs.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.AspNetCore.Mvc.ApiExplorer; | |
namespace Microsoft.AspNetCore.Mvc | |
{ | |
public static class DefaultApiConventions | |
{ | |
[ApiConventionNameMatch(ApiConventionNameMatchBehavior.Prefix)] | |
[ProducesDefaultResponseType] | |
[ProducesResponseType(201)] | |
[ProducesResponseType(400)] | |
public static void Create([ApiConventionNameMatch(ApiConventionNameMatchBehavior.Any)][ApiConventionTypeMatch(ApiConventionTypeMatchBehavior.Any)] object model); | |
[ApiConventionNameMatch(ApiConventionNameMatchBehavior.Prefix)] | |
[ProducesDefaultResponseType] | |
[ProducesResponseType(200)] | |
[ProducesResponseType(404)] | |
[ProducesResponseType(400)] | |
public static void Delete([ApiConventionNameMatch(ApiConventionNameMatchBehavior.Suffix)][ApiConventionTypeMatch(ApiConventionTypeMatchBehavior.Any)] object id); | |
[ApiConventionNameMatch(ApiConventionNameMatchBehavior.Prefix)] | |
[ProducesDefaultResponseType] | |
[ProducesResponseType(204)] | |
[ProducesResponseType(404)] | |
[ProducesResponseType(400)] | |
public static void Edit([ApiConventionNameMatch(ApiConventionNameMatchBehavior.Suffix)][ApiConventionTypeMatch(ApiConventionTypeMatchBehavior.Any)] object id, [ApiConventionNameMatch(ApiConventionNameMatchBehavior.Any)][ApiConventionTypeMatch(ApiConventionTypeMatchBehavior.Any)] object model); | |
[ApiConventionNameMatch(ApiConventionNameMatchBehavior.Prefix)] | |
[ProducesDefaultResponseType] | |
[ProducesResponseType(200)] | |
[ProducesResponseType(404)] | |
public static void Find([ApiConventionNameMatch(ApiConventionNameMatchBehavior.Suffix)][ApiConventionTypeMatch(ApiConventionTypeMatchBehavior.Any)] object id); | |
[ApiConventionNameMatch(ApiConventionNameMatchBehavior.Prefix)] | |
[ProducesDefaultResponseType] | |
[ProducesResponseType(200)] | |
[ProducesResponseType(404)] | |
public static void Get([ApiConventionNameMatch(ApiConventionNameMatchBehavior.Suffix)][ApiConventionTypeMatch(ApiConventionTypeMatchBehavior.Any)] object id); | |
[ApiConventionNameMatch(ApiConventionNameMatchBehavior.Prefix)] | |
[ProducesDefaultResponseType] | |
[ProducesResponseType(201)] | |
[ProducesResponseType(400)] | |
public static void Post([ApiConventionNameMatch(ApiConventionNameMatchBehavior.Any)][ApiConventionTypeMatch(ApiConventionTypeMatchBehavior.Any)] object model); | |
[ApiConventionNameMatch(ApiConventionNameMatchBehavior.Prefix)] | |
[ProducesDefaultResponseType] | |
[ProducesResponseType(204)] | |
[ProducesResponseType(404)] | |
[ProducesResponseType(400)] | |
public static void Put([ApiConventionNameMatch(ApiConventionNameMatchBehavior.Suffix)][ApiConventionTypeMatch(ApiConventionTypeMatchBehavior.Any)] object id, [ApiConventionNameMatch(ApiConventionNameMatchBehavior.Any)][ApiConventionTypeMatch(ApiConventionTypeMatchBehavior.Any)] object model); | |
[ApiConventionNameMatch(ApiConventionNameMatchBehavior.Prefix)] | |
[ProducesDefaultResponseType] | |
[ProducesResponseType(204)] | |
[ProducesResponseType(404)] | |
[ProducesResponseType(400)] | |
public static void Update([ApiConventionNameMatch(ApiConventionNameMatchBehavior.Suffix)][ApiConventionTypeMatch(ApiConventionTypeMatchBehavior.Any)] object id, [ApiConventionNameMatch(ApiConventionNameMatchBehavior.Any)][ApiConventionTypeMatch(ApiConventionTypeMatchBehavior.Any)] object model); | |
} | |
} |
