c# - ASP MVC ActionFilterAttribute OnActionExecuting 未触发

标签 c# asp.net asp.net-mvc redirecttoaction actionfilterattribute

我有 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");}

还有一个 ActionFilterAttributeOnActionExecuting 方法

 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/

相关文章:

c# - Xamarin 将 XAML ContextAction 绑定(bind)到 ViewCell 中的 C#

c# - 比较 'mobile numbers' - 和/或国际格式

使用来自 Windows 应用程序的表单例份验证的 ASP.NET Web 服务

c# - nlog 登录到另一个目录

asp.net-mvc - 为什么部署到 Azure 网站时,AllowAnonymous 不起作用?

c# - 如何使用 NLog 在当前目录中创建文本文件?

c# - 组合框样式显示评级和描述 WPF

c# - 从 arrayList 中获取值

asp.net - asp :datalist in asp.net-mvc 相当于什么

c# - 将 MSBuild 参数传递给 Cake 构建脚本以生成 _PublishedWebsites