如何从HttpContext获取ASP.NET Core MVC筛选器

问题描述:

我正在尝试编写一些中间件,并且需要知道当前操作方法(如果有)是否具有特定的筛选器属性,因此可以根据它的存在来更改行为。如何从HttpContext获取ASP.NET Core MVC筛选器

那么当您实施时,是否有可能像您在ResourceExecutingContext上那样获得类型为IList<IFilterMetadata>的过滤器集合?

今天不太可能。

注:不是直接回答你的问题,但可能会根据您的需求帮助(和代码是征求意见太长)

注2:不知道,如果它工作核心,如果它不,告诉我,我删除了答案

你可以知道,在过滤器中,如果另一个过滤器与一起使用:

public class OneFilter : ActionFilterAttribute, IActionFilter 
{ 
    void IActionFilter.OnActionExecuting(ActionExecutingContext filterContext) 
    { 
     // Check if the Attribute "AnotherFilter" is used 
     if (filterContext.ActionDescriptor.IsDefined(typeof(AnotherFilter), true) || filterContext.Controller.GetType().IsDefined(typeof(AnotherFilter), true)) 
     { 
      // things to do if the filter is used 

     } 
    } 
} 

public class AnotherFilter : ActionFilterAttribute, IActionFilter 
{ 
    // filter things 
} 

AND/OR

你可以把在路径数据中的一些数据,让知道这是用来过滤器的操作:

void IActionFilter.OnActionExecuting(ActionExecutingContext filterContext) 
{ 
    filterContext.RouteData.Values.Add("OneFilterUsed", "true"); 
    base.OnActionExecuting(filterContext); 
} 

...

public ActionResult Index() 
{ 
    if(RouteData.Values["OneFilterUsed"] == "true") 
    { 

    } 

    return View(); 
}