[英]Ignoring ActionFilterAttribute, although explicitly defined on controller / action
I've got an ASP.NET middleware in the form of ActionFilterAttribute
. 我有一个ActionFilterAttribute
形式的ASP.NET中间件。
For example: 例如:
[CacheResponse(Seconds = 3)]
public async Task<IHttpActionResult> Echo(string userId, string message, string queryString)
{
await Task.Delay(150);
return Ok(new {Action = "Echo", UserId = userId, Message = message, QueryString = queryString});
}
The attribute CacheResponse
is a nuget, I cannot touch its code. 属性CacheResponse
是一个nuget,我无法触摸其代码。 Yet I would like to have a feature-on/off configuration, so if I want to disable the cache mechanism, I won't have to do changes in the code. 但是我想启用/禁用功能,因此,如果我想禁用缓存机制,则无需更改代码。
How could I "cancel" the subscription of some controllers / actions to attributes? 如何才能“取消”某些控制器/动作对属性的订阅? even though it's explicitly decorated with it? 即使已明确装饰了它?
I'm looking for a piece of code to run on webrole startup, that given the configuration value for the feature-on/off would cancell the decoration. 我正在寻找一段在webrole启动时运行的代码,给定功能开/关的配置值可以取消装饰。
Thank you 谢谢
I have implemented Nikolaus's idea. 我已经实现了尼古拉斯的想法。 Code might look like: 代码可能如下所示:
public class ConfigurableCacheResponseAttribute : CacheResponseAttribute
{
//Property injection
public IApplicationConfig ApplicationConfig { get; set; }
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
if (this.ApplicationConfig.Get<bool>("CashingEnabled"))
{
base.OnActionExecuted(actionExecutedContext);
}
}
public override Task OnActionExecutedAsync(HttpActionExecutedContext actionExecutedContext, CancellationToken cancellationToken)
{
if (this.ApplicationConfig.Get<bool>("CashingEnabled"))
{
return base.OnActionExecutedAsync(actionExecutedContext, cancellationToken);
}
return Task.CompletedTask;
}
public override void OnActionExecuting(HttpActionContext actionContext)
{
if (this.ApplicationConfig.Get<bool>("CashingEnabled"))
{
base.OnActionExecuting(actionContext);
}
}
public override Task OnActionExecutingAsync(HttpActionContext actionContext, CancellationToken cancellationToken)
{
if (this.ApplicationConfig.Get<bool>("CashingEnabled"))
{
return base.OnActionExecutingAsync(actionContext, cancellationToken);
}
return Task.CompletedTask;
}
}
How to use dependency injection with filter attribute you could find here . 如何在过滤器属性中使用依赖注入,您可以在这里找到。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.