Understanding ASP.NET Core's conventions

The default response type of the Swagger UI produces some incorrect information. If we take a look at the responses section, we will see that the response code is incorrect and that it doesn't correspond with the actual HTTP code that's returned by the web service. When using ASP.NET Core 2.2 or later, it is possible to use conventions to specify the response types:

..
[ApiController]
public class ItemController : ControllerBase
{
[HttpGet]
[ApiConventionMethod(typeof(DefaultApiConventions),
nameof(DefaultApiConventions.Get))]

public async Task<IActionResult> Get([FromQuery] int pageSize =
10, [FromQuery] int pageIndex = 0)

[HttpGet("{id:guid}")]
[ApiConventionMethod(typeof(DefaultApiConventions),
nameof(DefaultApiConventions.Get))]

public async Task<IActionResult> GetById(Guid id)
...

For example, the preceding code uses the ApiConventionMethod attribute to pass a custom type and a method name. The ApiConventionMethod attribute is part of the Microsoft.AspNetCore.Mvc namespace and uses the DefaultApiConventions static class, which provides a default set of conventions for each action in a generic API. In the same way, we can add that attribute to the writing methods of ItemController, such as the Create, Update, and Delete methods:

        ...

[HttpPost]
[ApiConventionMethod(typeof(DefaultApiConventions),
nameof(DefaultApiConventions.Create))]

public async Task<IActionResult> Create(AddItemRequest request)

[HttpPut("{id:guid}")]
[ApiConventionMethod(typeof(DefaultApiConventions),
nameof(DefaultApiConventions.Update))]

public async Task<IActionResult> Update(Guid id,
EditItemRequest request)

[HttpDelete("{id:guid}")]
[ApiConventionMethod(typeof(DefaultApiConventions),
nameof(DefaultApiConventions.Delete))]

public async Task<IActionResult> Delete(Guid id)
}
}

This kind of approach is a shortcut that we can use to declare action method responses without explicitly using the ProducesResponseType attribute. Let's take a look at the DefaultApiConventions static class, which provides a set of default response types if we declare some static void methods:

using Microsoft.AspNetCore.Mvc.ApiExplorer;

namespace Microsoft.AspNetCore.Mvc
{
public static class DefaultApiConventions
{
[ProducesResponseType(200)]
[ProducesResponseType(404)]
[ProducesDefaultResponseType]
[ApiConventionNameMatch(ApiConventionNameMatchBehavior.Prefix)]
public static void Get([ApiConventionNameMatch
(ApiConventionNameMatchBehavior.Suffix), ApiConventionTypeMatch(
ApiConventionTypeMatchBehavior.Any)] object id)

{
}

...
}
}

For example, for the Get method, it states the HTTP 200 OK response and HTTP 404 Not found. By doing this, we can easily declare the proper response types for each action. The DefaultApiConventions class is part of the Microsoft.AspNetCore.Mvc namespace.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.22.248.208