ASP MVC ActionFilterAttribute OnActionExecuting not fired

对着背影说爱祢 提交于 2020-01-02 17:12:38

问题


I have 2 controllers Home with

public class HomeController : Controller
    {
    protected override void OnActionExecuting(ActionExecutingContext filterContext)
            {
                // do some irrelevant stuff
                    base.OnActionExecuting(filterContext);           

            }

    public ActionResult Index()
            {            
                    return View();
            }
}

and Service with

public ActionResult Confirm()
            { return RedirectToAction("Index", "Home");}

And one ActionFilterAttribute with OnActionExecuting method

 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());
        }
    }

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?

Thanks


回答1:


Refer this For More clarity

First of all Remove OnActionExecuting method in controller level

public class HomeController : Controller
{
       [InvitationModeAttribute]
     public ActionResult Index()
     {            
        return View();
     }
 }

2nd Controller

 public class ServiceController : Controller
 {
   [InvitationModeAttribute]
   public ActionResult Confirm()
   { 
     return RedirectToAction("Index", "Home");
   }
 }

From 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.



来源:https://stackoverflow.com/questions/31072880/asp-mvc-actionfilterattribute-onactionexecuting-not-fired

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!