首页 > 解决方案 > 在 C# MVC 中捕获所有插入更新事件

问题描述

我正在为我的应用程序使用 asp.net mvc、entitiy 框架和 sql db。我想为 mvc 应用程序中的所有插入更新删除事件创建一种方法,如过滤器作为事件侦听器。对此有什么建议吗?

提前致谢

标签: asp.net-mvc

解决方案


您可以创建一个自定义过滤器并用它装饰您想要的操作或控制器,如下所示:

public class CustomFilter : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {   
            //In case you need it to log which one has been accessed for example
            Controller controller = filterContext.Controller as Controller;

            //Do your code...

            //Continue to the action
            base.OnActionExecuting(filterContext);
        }
    }

推荐阅读