zoukankan      html  css  js  c++  java
  • Filter实现AOP

    1.添加一个特性attribute类

        public class CustomerControllerFilterAttribute : Attribute, IActionFilter  、、也可以实现IResourceFilter等
        {
            public void OnActionExecuting(ActionExecutingContext context) {
                Console.WriteLine($"This is {typeof(CustomerControllerFilterAttribute)} OnActionExecuting");
            }
    
            public void OnActionExecuted(ActionExecutedContext context)
            {
                Console.WriteLine($"This is {typeof(CustomerControllerFilterAttribute)} OnActionExecuted");
    
            }
    
        }

    2.在controller上添加特性

            [CustomerControllerFilter]
            public IActionResult Index()
            {
                var user = base.HttpContext.Session.GetString("CurrentUser");
                if (string.IsNullOrEmpty(user)) {
                    base.HttpContext.Session.SetString("CurrentUser", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                }
                base.ViewBag.User = base.HttpContext.Session.GetString("CurrentUser");
                return View();
            }
  • 相关阅读:
    write to logfile
    open and read a file content to a variable
    strategy
    Android 开机启动程序
    消息队列
    卡机音乐功能实现
    Android 2.0 开机动画文件分析
    多线程实例
    消息队列
    多线程实例
  • 原文地址:https://www.cnblogs.com/kingsmart/p/15518515.html
Copyright © 2011-2022 走看看