c# - 使用一个 From Action 方法覆盖控制器上的 ResourceFilter
问题描述
我正在使用 ASP.NET Core 2.2 我有一个带有一些参数的 ResourceFilter。我在我的控制器上使用 ResourceFilter。我想做的是在该控制器中的特定操作上使用具有不同参数的相同过滤器类型。
我有类似的东西:
public class MyRequestFilter : IResourceFilter
{
private readonly string[] _someValues;
MyRequestFilter(string[] someValues)
{
_someValues = someValues;
}
public void OnResourceExecuting(ResourceExecutingContext context)
{
// do some checks here using _someValues
}
}
我在我的控制器上使用它,例如:
[TypeFilter(typeof(MyRequestFilter),
IsReusable = false,
Arguments = new object[] {
new[]{"some value",
"another value"}})]
public class MyController : Controller
{
}
我想做的是在特定的操作方法上覆盖该过滤器。就像是:
[TypeFilter(typeof(MyRequestFilter),
IsReusable = false,
Arguments = new object[] {
new[]{"value for this action",
"another value just for this action"}})]
public async Task<IActionResult> MyAction()
{
}
当我尝试上述操作并向绑定到该操作的端点发出请求时,我看到 MyRequestFilter.OnResourceExecuting() 被一个实例调用,该实例具有传递到控制器上的属性中的属性。如果我注释掉控制器过滤器,那么我会看到带有我在方法中传递的值的过滤器。我的问题是 - 有没有办法告诉方法忽略此特定路由的控制器过滤器并使用在方法属性上定义的过滤器?任何意见,将不胜感激。非常感谢!
解决方案
我的问题是 - 有没有办法告诉方法忽略此特定路由的控制器过滤器并使用在方法属性上定义的过滤器
context.Filters
您可以在运行时动态更改。例如,
- 创建一个什么都不做的 nope 过滤器
class NopeFilter : IResourceFilter { public void OnResourceExecuted(ResourceExecutedContext context) { } public void OnResourceExecuting(ResourceExecutingContext context) { } }
在运行时检查条件并将其他此类过滤器替换为
NopeFilter
. 假设我们只想保留第一个,这是一个实现:public class MyRequestFilter : IResourceFilter { // ... fields and constructor // a helper method that will replace the extra MyRequestFilters with NopeFilter at runtime private void DistinctThisTypeFilters(IList<IFilterMetadata> filters){ var nopeFilter = new NopeFilter() ; var count = filters.Count(); var thisTypeFilterCount = filters.OfType<MyRequestFilter>().Count(); // the total number: filters of this type if(thisTypeFilterCount > 1){ var alreadyReplaced = 0; // the number that we have replaced till now for(var i = count -1 ; i > 0 ; i--){ // replace in reverse order var filter = filters[i]; if(filter.GetType()== typeof(MyRequestFilter)) { if(alreadyReplaced < thisTypeFilterCount -1 ){ filters[i]= nopeFilter; alreadyReplaced++; } } } } } public void OnResourceExecuting(ResourceExecutingContext context) { this.DistinctThisTypeFilters(context.Filters); // add this line so that only the first one will take effect. // do some checks here using _someValues ... } public void OnResourceExecuted(ResourceExecutedContext context) { } }
推荐阅读
- laravel - Laravel 与枢轴同步多对多关系
- c# - 为什么在尝试获取已登录用户的用户名以便我可以在共享的 LayoutView 中显示它时出现此错误
- javascript - response.json() 中的值计数来自哪里?
- swift - 使用 GeometryReader 调整窗口大小时在 SwiftUI 中调整矩形网格的大小
- bash - 如何通过 CGI 脚本运行 jar 文件
- authentication - 使用 AuthenticationManager.GetWebLoginClientContext 对用户进行身份验证后,是否可以确定用户的登录名?
- javascript - 我正在尝试在 javascript 生成的 html 中生成一个 onclick 方法,它给了我一些非常奇怪的结果
- c++ - 值未显示,并遇到异常。是什么赋予了?
- c# - Kestrel 服务器声称在端口中侦听,但它没有
- python - 似乎无法在 Python 中交换记录数组中的行