我有 2 个 Controller Home
和
public class HomeController : Controller
{
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
// do some irrelevant stuff
base.OnActionExecuting(filterContext);
}
public ActionResult Index()
{
return View();
}
}
和服务
与
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
被调用时,OnActionExecuting
没有被解雇。
我怎样才能在 RedirectToAction
之后捕捉到它?
谢谢
最佳答案
引用this为了更清楚
首先 删除 Controller 级别的 OnActionExecuting 方法
public class HomeController : Controller
{
[InvitationModeAttribute]
public ActionResult Index()
{
return View();
}
}
第二个 Controller
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.
关于c# - ASP MVC ActionFilterAttribute OnActionExecuting 未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31072880/