zoukankan      html  css  js  c++  java
  • MVC 行为过滤器

    using FilterExam.Fiter;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;


    namespace FilterExam.Controllers
    {
        public class HelloController : Controller
        {
            [MyAction] 
            [MyResult]

            // GET: Hello
            public ActionResult Index()
            {
                Response.Write("行为执行中<br>");
                return View();
            }
        }
    }

    //行为过滤器

    在行为被执行前、后执行的过滤器
    重写方法OnActionExecuting(行为执行前)
    重写方法OnActionExecuted(行为执行后)


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;


    namespace FilterExam.Fiter
    {
        public class MyAction:ActionFilterAttribute
        {
            public override void OnActionExecuting(ActionExecutingContext filterContext)
            {            
                filterContext.HttpContext.Response.Write("ing<br>");
            }


            public override void OnActionExecuted(ActionExecutedContext filterContext)
            {           
                filterContext.HttpContext.Response.Write("ed<br>");
            }
        }

    }


    //结果过滤器

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;


    namespace FilterExam.Fiter
    {
        public class MyResult:ActionFilterAttribute
        {
            public override void OnResultExecuting(ResultExecutingContext filterContext)
            {
                filterContext.HttpContext.Response.Write("结果执行前<br>");
            }


            public override void OnResultExecuted(ResultExecutedContext filterContext)
            {
                filterContext.HttpContext.Response.Write("结果执行后<br>");


            }
        }

    }


    ing
    行为执行中
    ed
    结果执行前

    Hello

    结果执行后
  • 相关阅读:
    POJ
    POJ
    HDU
    HDU
    HDU
    POJ
    HDU
    POJ
    A-meeting 2019牛客暑期多校第四场 (树的直径)
    算法与数据结构实验题 2.1 塔防
  • 原文地址:https://www.cnblogs.com/dxmfans/p/9434745.html
Copyright © 2011-2022 走看看