2017年9月8日 星期五

[MVC] Action Filter

  • Action Filter 主要有下列的功能
    • Cross-cutting concerns 
      • Authorization
      • Logging
      • Caching
    • Built-in Action Filter
      • Authorize
      • Action
      • HandleError
  • 設計 Filter:繼承 ActionFilterAttribute 
    • ActionFilterAttribute 提供下列四種方法
      • OnActionExecuting – Action 之前執行 
      • OnActionExecuted – Action 之後執行 
      • OnResultExecuting – Action Result 之前執行 
      • OnResultExecuted – Action Result 之後執行
    • 步驟1:創建 LogActionFilter 
    • 步驟2:使用 ActionFilterAttribute 

using System.Web.Mvc;
using System.Diagnostics;
using System.Web.Routing;

namespace DemoError.LogAction
{
    public class LogActionFilter:ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            Log("OnActionExecuting", filterContext.RouteData);
        }

        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            Log("OnActionExecuted", filterContext.RouteData);
        }

        public override void OnResultExecuting(ResultExecutingContext filterContext)
        {
            Log("OnResultExecuting", filterContext.RouteData);
        }

        public override void OnResultExecuted(ResultExecutedContext filterContext)
        {
            Log("OnResultExecuted", filterContext.RouteData);
        }

        private void Log(string methodName, RouteData routeData)
        {
            var controllerName = routeData.Values["controller"];
            var actionName = routeData.Values["action"];
            Debug.WriteLine(string.Format($"{methodName}: controller:{controllerName}, action:{actionName} "));
        }
    }
}
    • 步驟3:使用 LogActionFilter
      • HomeController Index 加入 [LogActionFilter]
using DemoError.LogAction;
using System.Web.Mvc;

namespace DemoError.Controllers
{
    public class HomeController : Controller
    {
        [LogActionFilter]
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
    }
}
    • 步驟3-1
      • 如果需使整 Controller 都使用 LogActionFilter,調整 [LogActionFilter] 至 Controller 頂端
using DemoError.LogAction;
using System.Web.Mvc;

namespace DemoError.Controllers
{
    [LogActionFilter]
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
    }
}
    • 步驟3-2
      • 如果需使整個網站都使用 LogActionFilter,需再 Global.asax 中 Application_Start 進行註冊

using DemoError.LogAction;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;

namespace DemoError
{
    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            // 事件註冊
            GlobalFilters.Filters.Add(new LogActionFilter());
        }
    }
}

沒有留言:

張貼留言