• mvc Action Filter


    mvc Action Filter中OnActionExecuting、OnActionExecuted、OnResultExecuting、OnResultExecuted的执行顺序

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using System.IO;
    
    namespace MvcApp.ActionFilters
    {
        public class SimpleFilterAttribute:ActionFilterAttribute
        {
            public int ID { get; set; }
    
            public override void OnActionExecuting(ActionExecutingContext filterContext)
            {
                File.AppendAllText(@"D:ActionFilterTest.log", "SimpleFilter " + this.ID + " OnActionExecuting! 
    ");
            }
    
            public override void OnActionExecuted(ActionExecutedContext filterContext)
            {
                File.AppendAllText(@"D:ActionFilterTest.log", "SimpleFilter " + this.ID + " OnActionExecuted! 
    ");
            }
    
            public override void OnResultExecuting(ResultExecutingContext filterContext)
            {
                File.AppendAllText(@"D:ActionFilterTest.log", "SimpleFilter " + this.ID + " OnResultExecuting! 
    ");
            }
    
            public override void OnResultExecuted(ResultExecutedContext filterContext)
            {
                File.AppendAllText(@"D:ActionFilterTest.log", "SimpleFilter " + this.ID + " OnResultExecuted! 
    ");
            }
        }
    }
    View Code

         [SimpleFilter(ID = 1, Order = 1)]
            public ActionResult Index()
            {
                ViewData["Message"] = "Get the Filter Data!";
                return View();
            }

    这样的执行的结果是

    OnActionExecuted可以处理执行过程中的异常

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using System.Web.Routing;
    
    namespace MvcApp.ActionFilters
    {
        public class ErrorHandlingFilterAttribute:ActionFilterAttribute
        {
            public override void OnActionExecuted(ActionExecutedContext filterContext)
            {
                if (filterContext.Exception != null)
                {
                    filterContext.ExceptionHandled = true;
                    //Filter是Controllers的名称
                    filterContext.Result = new RedirectToRouteResult("Filter", new RouteValueDictionary(new { action = "OnError" }));
                }
            }
        }
    }
    View Code

    新建一个OnError的视图

    public ActionResult OnError()
            {
                ViewData["Title"] = "OnError";
                ViewData["Message"] = "Error Page!";
                return View();
            }
    
            [ErrorHandlingFilter]
            public void RaiseError()
            {
                throw new Exception();
            }
    View Code

    OnResultExecuting可以用于服务器端的压缩和客户端缓存

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using System.IO.Compression;
    
    namespace MvcApp.ActionFilters
    {
        public class CompressFilterAttribute:ActionFilterAttribute
        {
            public override void OnResultExecuting(ResultExecutingContext filterContext)
            {
                string accpetEncoding = filterContext.HttpContext.Request.Headers["Accept-Encoding"];
                if (string.IsNullOrEmpty(accpetEncoding))
                    return;
                var response = filterContext.HttpContext.Response;
                accpetEncoding=accpetEncoding.ToUpperInvariant();
                if (accpetEncoding.Contains("GZIP"))
                {
                    response.AddHeader("content-encoding", "gzip");
                    response.Filter = new GZipStream(response.Filter, CompressionMode.Compress);
                }
                else if (accpetEncoding.Contains("DEFLATE"))
                {
                    response.AddHeader("content-encoding", "deflate");
                    response.Filter = new DeflateStream(response.Filter, CompressionMode.Compress);
                }
                
            }
        }
    }
    View Code

    新建一个Compress的视图,可以压缩前后的大小

       [CompressFilter]
            public ActionResult Compress()
            {
                return View();
            }

    OnResultExecuting可以用于页面尾部输出信息和异常处理

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    
    namespace MvcApp.ActionFilters
    {
        public class TraceFilterAttribute:ActionFilterAttribute
        {
    
            public override void OnResultExecuting(ResultExecutingContext filterContext)
            {
                filterContext.HttpContext.Items["Start"] = DateTime.Now;
            }
    
            public override void OnResultExecuted(ResultExecutedContext filterContext)
            {
                DateTime start = (DateTime)filterContext.HttpContext.Items["Start"];
                TimeSpan ts = DateTime.Now - start;
                filterContext.HttpContext.Response.Write("<hr />" + ts.ToString());
            }
        }
    }
    View Code

    新建一个Timing的视图 

          [TraceFilter]
            public ActionResult Timing()
            {
                Random rdm = new Random(DateTime.Now.Millisecond);
                Thread.Sleep(rdm.Next(5) * 3000);
                return View();
            }

  • 相关阅读:
    .Net开发中IIS启动后,网站不能访问。0x80004005 无法执行程序。所执行的命令为 .....问题
    .Net开发中项目无法引用/项目引用失败/引用文件出现黄色警告图标,并生成失败的问题
    Mac系统安装Docker
    小程序开发----微信小程序实现下拉刷新功能
    小程序开发----微信小程序navigate跳转没反应
    小程序开发----小程序点击复制文本内容到剪贴板
    小程序开发----微信小程序直接写&nbsp;设置空格无效
    python字符串的反转
    python2项目迁移到python3
    robotframework废弃的关键词以及替代的关键词(关键词找不到,可能已经过期了)
  • 原文地址:https://www.cnblogs.com/ZJ199012/p/4146632.html
Copyright © 2020-2023  润新知