我需要从appsettings.json文件中获取ActionFilterAttribute中的值。我遵循了这个链接(How can I access AppSettings from an ActionFilterAttribute in ASP.NET Core),但它对我没有帮助,因为我需要将参数传递给我的自定义过滤器,并使用来自appsettings.json的一些参数。例如
//Filter.cs
[AttributeUsage(AttributeTargets.Method)]
public class ThrottleAttribute : ActionFilterAttribute
{
public string Count { get; set; }
private static MemoryCache Cache { get; } = new MemoryCache(new MemoryCacheOptions());
public override void OnActionExecuting(ActionExecutingContext actionExecutingContext)
{
//Compare this.Count with GetValueFromAppSettings.json
}
}//Controller.cs .Controller.cs
[ApiController]
[Route("api/[controller]")]
public class ValuesController : BaseController
{
// GET api/values
[HttpGet]
[Authorize]
[Throttle(Count = 15)]
public ActionResult<IEnumerable<string>> Get()
{
return new[] { "value1", "value2" };
}
}发布于 2018-06-28 19:40:12
在您的例子中,IFilterFactory实现将是最佳选择。
public class ThrottleAttributeFactory : Attribute, IFilterFactory
{
public string Count { get; set; }
public bool IsReusable => false;
public IFilterMetadata CreateInstance(IServiceProvider serviceProvider)
{
var filter = serviceProvider.GetService<ThrottleAttribute>();
filter.Count = Count;
return filter;
}
}然后,您可以将它注入构造函数。
private readonly IConfiguration config;
public ThrottleAttribute(IConfiguration config)
{
this.config = config;
}您必须将[Throttle(Count = 15)]更改为[ThrottleAttributeFactory(Count = 15)]
不要忘记在IConfiguration中注册Startup.cs和属性:
public void ConfigureServices(IServiceCollection services)
{
...
services.AddSingleton<IConfiguration>(Configuration);
services.AddScoped<ThrottleAttribute>();
}发布于 2018-06-28 19:14:46
appsettings.json应该在DI中注册,这样您就可以将IConfiguration传递给ThrottleAttribute构造函数并通过DI从它获得值,但是您需要创建工厂才能使其工作:
public class ThrottleAttributeFactory: ActionFilterAttribute, IFilterFactory
{
public string Count { get; set; }
public bool IsReusable => false;
public IFilterMetadata CreateInstance(IServiceProvider serviceProvider)
{
var attribute = serviceProvider.GetService<ThrottleAttribute>();
attribute.Count = Count;
return attribute;
}
}属性:
public class ThrottleAttribute : ActionFilterAttribute
{
private readonly IConfiguration _config;
public string Count { get; set; }
public ThrottleAttribute(IConfiguration config)
{
_config = config;
}
//rest of code omitted
}用你的方法:
{
var value = _config.GetValue<int>("json:key");
}在控制器中,用[Throttle(Count = 15)]代替[ThrottleAttributeFactory(Count = 15)]。
还注册属性:services.AddScoped<ThrottleAttribute>();
发布于 2018-06-28 19:26:11
您可以向Throttle类添加一个静态属性,并将配置值存储在其中。然后,在您的启动类中,可以设置该值。
[AttributeUsage(AttributeTargets.Method)]
public class ThrottleAttribute : ActionFilterAttribute
{
public string Count { get; set; }
public static string ConfigCount { get; set; }
private static MemoryCache Cache { get; } = new MemoryCache(new MemoryCacheOptions());
public override void OnActionExecuting(ActionExecutingContext actionExecutingContext)
{
//Compare this.Count with GetValueFromAppSettings.json
}
}Startup.cs
public void ConfigureServices(IServiceCollection services)
{
ThrottleAttribute.ConfigCount = Configuration.GetValue<int>("key");
}https://stackoverflow.com/questions/51089584
复制相似问题