Combining attributes ASP.NET Core
Say I want to apply the following attributes to all of my API controllers:
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:
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.3 Replies
So I just read this https://learn.microsoft.com/en-us/aspnet/core/mvc/controllers/application-model?view=aspnetcore-6.0#modify-the-controllermodel-description and made this
which surprisingly totally works. I have no idea what I'm doing tho.
However, it doesn
Work with the application model in ASP.NET Core
Learn how to read and manipulate the application model to modify how MVC elements behave in ASP.NET Core.
it doesn't work with ApiController
It seems i gets resolved before all that, and I don't even know if what I'm doing is adequate at all
So first, is there a way to apply
ApiController
from within that IControllerModelConvention
, and secondly, is what I'm doing even reasonable?
Ok so [ApiController]
requires at least one IRouteTemplateProvider
attribute be present, and the api behavior is actually enabled by IApiBehaviorMetadata
, so I combined these into one to both enable the default route and enable api conventions, which works. No base classes, no boilerplate.
There's also this thing https://learn.microsoft.com/en-us/aspnet/core/web-api/?view=aspnetcore-2.1#attribute-on-an-assembly-3
Create web APIs with ASP.NET Core
Learn the basics of creating a web API in ASP.NET Core.