未触发ASP MVC ActionFilterAttribute OnActionExecuting [英] ASP MVC ActionFilterAttribute OnActionExecuting not fired
问题描述
我有2个控制器 Home
,并且
public class HomeController : Controller
{
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
// do some irrelevant stuff
base.OnActionExecuting(filterContext);
}
public ActionResult Index()
{
return View();
}
}
和服务
with
public ActionResult Confirm()
{ return RedirectToAction("Index", "Home");}
和一个 ActionFilterAttribute
和 OnActionExecuting
方法
public class InvitationModeAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
// do some stuff
base.OnActionExecuting(filterContext);
}
}
public class FilterConfig
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new InvitationModeAttribute());
}
}
当我去 localhost / Service / Confirm
, OnActionExecuting
被触发,但是随后当调用 RedirectToAction
时,<没有触发code> OnActionExecuting 。
在 RedirectToAction
之后如何捕捉?
When I go to localhost/Service/Confirm
, OnActionExecuting
is fired, but then when RedirectToAction
is called, OnActionExecuting
is not fired.
How can I catch this after RedirectToAction
?
谢谢
推荐答案
引用此为了更加清晰
首先
删除控制器级别的OnActionExecuting方法
First of all Remove OnActionExecuting method in controller level
public class HomeController : Controller
{
[InvitationModeAttribute]
public ActionResult Index()
{
return View();
}
}
第二个控制器
public class ServiceController : Controller
{
[InvitationModeAttribute]
public ActionResult Confirm()
{
return RedirectToAction("Index", "Home");
}
}
从MSDN
动作过滤器
Scope of Action Filters
除了使用动作
过滤器标记单个动作方法外,您还可以使用动作
过滤器将整个控制器类标记为一个整体。在这种情况下,过滤器将应用于该
控制器的所有操作方法。另外,如果您的控制器派生自另一个
控制器,则基本控制器可能具有自己的动作过滤器
属性。同样,如果您的控制器从基本控制器覆盖了操作方法
,则该方法可能具有自己的操作过滤器
属性,并且这些属性是从覆盖的操作方法继承的。为了使
易于理解动作过滤器如何协同工作,动作
方法被分组到作用域中。范围定义了属性
的应用位置,例如它是标记类还是方法,以及
是标记基类还是派生类。
In addition to marking individual action methods with an action filter, you can mark a controller class as a whole with an action filter. In that case, the filter applies to all action methods of that controller. Additionally, if your controller derives from another controller, the base controller might have its own action-filter attributes. Likewise, if your controller overrides an action method from a base controller, the method might have its own action-filter attributes and those it inherits from the overridden action method. To make it easier to understand how action filters work together, action methods are grouped into scopes. A scope defines where the attribute applies, such as whether it marks a class or a method, and whether it marks a base class or a derived class.
这篇关于未触发ASP MVC ActionFilterAttribute OnActionExecuting的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!