I have very simple question that how can I resolve dependency
in custom attributes
public class CustomRouteAttribute : Attribute, IRouteTemplateProvider
{
string serverKey = string.Empty;
// Parameterised constructor
// Need to resolve it automatically without passing it explicitly
public CustomRouteAttribute(IConfiguration configuration)
{
serverKey = configuration.GetSection("serverkey").Value;
}
public string Template => $"api/{serverKey}/[controller]";
public int? Order { get; set; }
public string Name { get; set; }
}
I know without parameterised constructor
I can use it without any problem like this:
[CustomRoute]
[ApiController]
public class DemoController : ControllerBase
{
// Code....
}
So my question is: How can I use dependency injection
in custom attributes
.
I know there could be a way like we do in custom filters
. We can use dependency injection in our custom filter
and use it with TypeFilterAttribute or ServiceFilterAttribute
Example [TypeFilter(typeof(ExampleActionFilter))]