c# - .Net Core 自定义属性中的依赖注入
问题描述
dependency
我有一个非常简单的问题,我该如何解决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; }
}
我知道没有parameterised constructor
我可以毫无问题地使用它:
[CustomRoute]
[ApiController]
public class DemoController : ControllerBase
{
// Code....
}
所以我的问题是:我如何dependency injection
在custom attributes
.
我知道可能有一种方法,就像我们在custom filters
. 我们可以在我们的中使用依赖注入custom filter
并将其与TypeFilterAttribute或ServiceFilterAttribute一起使用
例子 [TypeFilter(typeof(ExampleActionFilter))]
解决方案
推荐阅读
- python - 在为模型训练选择训练测试拆分时,它是否从所有类中选择偶数个样本?
- r - 使用 scan() 读取 R 中的多个 .txt 文件
- python - Python列表到csv文件
- r - 加速 R 中的嵌套循环
- ckfinder - 将 Ckfinder 与 Serenity 平台集成
- python - 遍历字符串元组列表
- c++ - 在 if constexpr 分支中产生编译器错误的惯用方法
- .htaccess - .htaccess 阻止热链接图像(在我自己的站点上阻止)
- amazon-web-services - S3 和 AWS CloudFront 的持续交付问题
- reactjs - 重定向时关闭 iframe