// 自定义attribute 方式
public class CustomIOCFilterFactoryAttribute : Attribute, IFilterFactory
{
public bool IsReusable => true;
private readonly Type _type;
public CustomIOCFilterFactoryAttribute(Type type)
{
_type = type;
}
public IFilterMetadata CreateInstance(IServiceProvider serviceProvider)
{
return (IFilterMetadata)serviceProvider.GetService(_type);
}
}
使用方法
[CustomIOCFilterFactoryAttribute(typeof(CustomExceptionFiter))]
public IActionResult Index()
{
逻辑部分
}