• 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:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    
    namespace TestMvcWebApp1.ActionResultEx
    {
        public class ShowMessageAttribute:ActionFilterAttribute
        {
            public string Message { get; set; }
            public override void OnActionExecuting(ActionExecutingContext filterContext)
            {
                filterContext.HttpContext.Response.Write("[BeforeAction " + Message + "]");
            }
            public override void OnActionExecuted(ActionExecutedContext filterContext)
            {
                filterContext.HttpContext.Response.Write("[AfterAction " + Message + "]");
            }
            public override void OnResultExecuting(ResultExecutingContext filterContext)
            {
                filterContext.HttpContext.Response.Write("[BeforeResult " + Message + "]");
            }
            public override void OnResultExecuted(ResultExecutedContext filterContext)
            {
                filterContext.HttpContext.Response.Write("[AfterResult " + Message + "]");
            }
        }
    }
    
    

    使用:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using TestMvcWebApp1.ActionResultEx;
    using System.Data.SqlClient;
    
    namespace TestMvcWebApp1.Controllers
    {
        public class ShowMessageDemoController : Controller
        {
            //
            // GET: /ShowMessageDemo/
            [ShowMessage(Message = "这是我的信息A" , Order=1)]
            [HandleError(View="Problem")]
            public ActionResult Index()
            {
                int a = 0;
                SqlConnection conn = new SqlConnection();
                conn.Open();
    
                return Content("  网页内容  ");
            }
    
        }
    }
    
    

    输出结果:

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

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

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

  • 相关阅读:
    python自定义编写有关用户登录注册程序代码
    项目经理多年的经验之谈
    Linux虚拟机克隆后,启动系统发现网卡无法启动
    mysql出现服务器异常后,重启服务器后无法开启数据库处理方法
    计算机毕业四年,我都做了什么?
    个人Blog(采用Django+uwsgi+nginx)里面包含很多技术文章
    Delphi to C# Equivalancesdelphi和C#类似的地方
    Javascript浏览器关于scrollLeft,scrollTop的兼容性
    Mozilla Firefox15怎么样才能把标签页弄到下面去,就和360的一样,Mozilla Firefox15没有取消标签置顶这个选项……
    Delphi过程函数传递参数的几种方式
  • 原文地址:https://www.cnblogs.com/wucg/p/1912357.html
Copyright © 2020-2023  润新知