• Asp.Net MVC 页面代码压缩筛选器-自定义删除无效内容


    Asp.Net MVC 页面代码压缩筛选器

    首先定义以下筛选器,用于代码压缩。

    /*页面压缩 筛选器*/
        public class WhiteSpaceFilter : Stream
        {
            private Stream _shrink;
            private Func<string, string> _filter;
    
            public WhiteSpaceFilter(Stream shrink, Func<string, string> filter)
            {
                _shrink = shrink;
                _filter = filter;
            }
    
    
            public override bool CanRead { get { return true; } }
            public override bool CanSeek { get { return true; } }
            public override bool CanWrite { get { return true; } }
            public override void Flush() { _shrink.Flush(); }
            public override long Length { get { return 0; } }
            public override long Position { get; set; }
            public override int Read(byte[] buffer, int offset, int count)
            {
                return _shrink.Read(buffer, offset, count);
            }
            public override long Seek(long offset, SeekOrigin origin)
            {
                return _shrink.Seek(offset, origin);
            }
            public override void SetLength(long value)
            {
                _shrink.SetLength(value);
            }
            public override void Close()
            {
                _shrink.Close();
            }
    
            public override void Write(byte[] buffer, int offset, int count)
            {
                // capture the data and convert to string
                byte[] data = new byte[count];
                Buffer.BlockCopy(buffer, offset, data, 0, count);
                string s = Encoding.GetEncoding("gb2312").GetString(buffer);
    
                // filter the string
                s = _filter(s);
    
                // write the data to stream
                byte[] outdata = Encoding.GetEncoding("gb2312").GetBytes(s);
                _shrink.Write(outdata, 0, outdata.GetLength(0));
            }
        }
    
        public class WhitespaceFilterAttribute : ActionFilterAttribute
        {
            public override void OnActionExecuting(ActionExecutingContext filterContext)
            {
                var request = filterContext.HttpContext.Request;
                var response = filterContext.HttpContext.Response;
                response.Filter = new WhiteSpaceFilter(response.Filter, s =>
                {
                    s = Regex.Replace(s, @"s+(?=<)|s+$|(?<=>)s+|<!--.+-->|[ ]+//.+
    |/*.+*/|
    ", "");
    
                    //single-line doctype must be preserved
                    var firstEndBracketPosition = s.IndexOf(">");
                    if (firstEndBracketPosition >= 0)
                    {
                        s = s.Remove(firstEndBracketPosition, 1);
                        s = s.Insert(firstEndBracketPosition, ">");
                    }
                    return s;
                });
            }
        }
        /*页面压缩 筛选器 end*/

    在要压缩页面的action上使用。

    [WhitespaceFilter]
    public ActionResult home()
    {
          return View();
    }

    我们可以自行修改替换规则,以达到最优化。

    s = Regex.Replace(s, @"s+(?=<)|s+$|(?<=>)s+|<!--.+-->|[ ]+//.+
    |/*.+*/|
    ", "");
  • 相关阅读:
    ES学习(十)
    ES学习(九)
    ES学习(八)
    ES学习(七)--documentAPI
    ES学习(六)--shard和replica机制
    ES学习(四)--嵌套聚合、下钻分析、聚合分析
    uniapp中常见的请求方法封装 --来之插件市场(全局方法封装(请求/正则/URI)
    工具/插件
    express中文件的上传 multer
    express中开发常用
  • 原文地址:https://www.cnblogs.com/huhangfei/p/5000735.html
Copyright © 2020-2023  润新知