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

    结果执行后
  • 相关阅读:
    开始学习C#
    关于串口数据读取的几个问题
    Joel测试
    VC查找内存泄漏技巧【转】
    思考题一
    自我介绍
    2020面向对象程序设计寒假作业1 题解
    思考题二
    题解 洛谷P2158 【[SDOI2008]仪仗队】
    深入浅出InfoPath系列
  • 原文地址:https://www.cnblogs.com/dxmfans/p/9434745.html
Copyright © 2011-2022 走看看