zoukankan      html  css  js  c++  java
  • asp.net mvc filter

    Using Filters to Attach Reusable Behaviors

    Introducing the Four Basic Types of Filters

     

    Notice that ActionFilterAttribute is the default implementation for both IActionFilter
    and IResultFilter—it implements both of those interfaces. It’s meant to be totally general
    purpose, so it doesn’t provide any implementation (in fact, it’s marked abstract, so you can
    only use it by deriving a subclass from it). However, the other default implementations
    (AuthorizeAttribute and HandleErrorAttribute) are concrete, contain useful logic, and can
    be used without deriving a subclass.
    To get a better understanding of these types and their relationships, examine Figure 9-6.
    It shows that all filter attributes are derived from FilterAttribute, and also implement one or
    more of the filter interfaces. The dark boxes represent ready-to-use concrete filters; the rest are
    interfaces or abstract base classes. Later in this chapter, you’ll learn more about each built-in
    filter type.

    MyFilter Demo:

    01using System;
    02using System.Collections.Generic;
    03using System.Linq;
    04using System.Web;
    05using System.Web.Mvc;
    06 
    07namespace TestMvcWebApp1.ActionResultEx
    08{
    09    public class ShowMessageAttribute:ActionFilterAttribute
    10    {
    11        public string Message { get; set; }
    12        public override void OnActionExecuting(ActionExecutingContext filterContext)
    13        {
    14            filterContext.HttpContext.Response.Write("[BeforeAction " + Message + "]");
    15        }
    16        public override void OnActionExecuted(ActionExecutedContext filterContext)
    17        {
    18            filterContext.HttpContext.Response.Write("[AfterAction " + Message + "]");
    19        }
    20        public override void OnResultExecuting(ResultExecutingContext filterContext)
    21        {
    22            filterContext.HttpContext.Response.Write("[BeforeResult " + Message + "]");
    23        }
    24        public override void OnResultExecuted(ResultExecutedContext filterContext)
    25        {
    26            filterContext.HttpContext.Response.Write("[AfterResult " + Message + "]");
    27        }
    28    }
    29}

    使用:

    01using System;
    02using System.Collections.Generic;
    03using System.Linq;
    04using System.Web;
    05using System.Web.Mvc;
    06using TestMvcWebApp1.ActionResultEx;
    07using System.Data.SqlClient;
    08 
    09namespace TestMvcWebApp1.Controllers
    10{
    11    public class ShowMessageDemoController : Controller
    12    {
    13        //
    14        // GET: /ShowMessageDemo/
    15        [ShowMessage(Message = "这是我的信息A" , Order=1)]
    16        [HandleError(View="Problem")]
    17        public ActionResult Index()
    18        {
    19            int a = 0;
    20            SqlConnection conn = new SqlConnection();
    21            conn.Open();
    22 
    23            return Content("  网页内容  ");
    24        }
    25 
    26    }
    27}

    输出结果:

    [BeforeAction 这是我的信息A][AfterAction 这是我的信息A]

    [BeforeResult 这是我的信息A]  网页内容  [AfterResult 这是我的信息A]

     <customErrors mode="On"></customErrors>

  • 相关阅读:
    单进程架构数据库谨防隐形杀手
    21.2 超时与重传的简单例子
    19日下午三点直播:DevOps体系中数据库端的四大问题及解决之道
    SQL无所不能:DBA宝妈宝爸系列分享
    用Excel做了7天报表,这个领导喜欢的可视化工具,只用了7小时
    从块结构谈表的存储参数与性能之间的关系
    MYSQL SHELL 到底是个什么局 剑指 “大芒果”
    大数据构架师经典学习宝典
    POJ 3171 区间最小花费覆盖 (DP+线段树
    POJ 3171 区间最小花费覆盖 (DP+线段树
  • 原文地址:https://www.cnblogs.com/goody9807/p/2203934.html
Copyright © 2011-2022 走看看