C#C
C#4y ago
6 replies
Anton

Combining attributes ASP.NET Core

Say I want to apply the following attributes to all of my API controllers:
[ApiController]
[Route("api/[controller]")]
[Produces(MediaTypeNames.Application.Json)]
[Consumes(MediaTypeNames.Application.Json)]

Now I know there's no way in pure C# to combine multiple attributes into one, or have a function or a class that returns or represents multiple attributes. I also know that the only way to kinda combine them in pure C# would be to make a base class and inherit from it:
[ApiController]
[Route("api/[controller]")]
[Produces(MediaTypeNames.Application.Json)]
[Consumes(MediaTypeNames.Application.Json)]
public abstract class ApiControllerBase : Controller
{
}

which is bad for obvious reasons.
Is there maybe some
IControllerMetadataProvider
sorta thing in ASP.NET Core that could effectively allow me to combine the attributes, or is inheritance / repetition the only way?
Is there maybe a way to add this default configuration to all controllers from a certain assembly? enlighten me, I'm new to the framework.
Was this page helpful?