Last week I talked about the [ApiExplorer]
attribute and how it can simplify your controller code when implementing Web API's.
Today I have a small tip for you, did you know it isn't necessary to annotate every controller with the [ApiExplorer]
attribute?
It is also possible to apply the attribute to an assembly. When the [ApiController]
attribute is applied to an assembly, all controllers in the assembly have the [ApiController]
attribute applied.
Therefore apply the assembly-level attribute to the Program.cs
file:
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; | |
//Applies the ApiController attribute to all your controllers | |
[assembly: ApiController] | |
var builder = WebApplication.CreateBuilder(args); | |
builder.Services.AddControllers(); | |
var app = builder.Build(); | |
app.UseHttpsRedirection(); | |
app.UseAuthorization(); | |
app.MapControllers(); | |
app.Run(); |