.NET MVC项目压缩HTML、CSS、JSS

需要压缩的页面继承BaseController,也可以参照转载原文重载RazorViewEngine的方法:

 public class BaseController:Controller
    {

        #region 页面压缩
        private HtmlTextWriter _htmlWriter;

        private StringWriter _writer;

        private StringBuilder _builder;

        private HttpWriter _output;

        private static Minifier minifier = new Minifier();
        private static CodeSettings codeSettings = new CodeSettings()
        {
            IgnoreAllErrors = true,
            MinifyCode = true,
            LocalRenaming = LocalRenaming.CrunchAll
        };
        private static CssSettings cssSettings = new CssSettings()
        {
            IgnoreAllErrors = true,
            OutputMode = OutputMode.SingleLine,
            MinifyExpressions = true,
        };
         //匹配js
        private static Regex regexJs = new Regex("(?<=<script(.)*?>)([\\s\\S](?!<script))*?(?=</script>)", RegexOptions.IgnoreCase);
        //string regexstr=@"<script[^>]*>([\s\S](?!<script))*?</script>";

        /// <summary>
        /// 匹配<style>xxxxx</style>之间的内容
        /// </summary>
        //private static Regex regexStyle = new Regex("(?<=<style(.*?)>)(.|\n)*?(?=</style>)", RegexOptions.IgnoreCase);
        private static Regex regexStyle = new Regex("(?<=<style(.)*?>)([\\s\\S](?!<script))*?(?=</style>)", RegexOptions.IgnoreCase);
        private const string keyCss = "[css*.256.843.56.745.h*J.]";
        private const string keyJs = "[js*.869.218.839.*W.]";

        /// <summary>
        ///压缩html、css、js代码
        /// </summary>
        /// <param name="text">html代码</param>
        /// <returns></returns>
        private static string Compress(string html, ResultExecutedContext filterContext)
        {
            if (filterContext.HttpContext.Request.RequestType.ToUpper() == "GET")
            {
                Parallel.Invoke(() =>
                {
                //压缩js           
                var marchCollection = regexJs.Matches(html);
                    if (marchCollection.Count > 0)
                    {
                        foreach (Match item in marchCollection)
                        {
                            if (string.IsNullOrWhiteSpace(item.Value))
                            {
                                continue;
                            }
                        //string jsTemp = item.Value.Replace("<script>", "").Replace("</script>","").Replace("<script type=\"text/javascript\">", "");
                        html = html.Replace(item.Value, keyJs);
                        //压缩js
                        string minJs = minifier.MinifyJavaScript(item.Value, codeSettings);
                            html = html.Replace(keyJs, minJs);
                        }
                    }
                }
                 , () =>
                 {
                 //压缩css
                 //是否有<style>标签
                 //var isHaveStyle = Regex.Match(sb.ToString(), "<style(.*)>", RegexOptions.IgnoreCase).Success;
                 //匹配style节点         
                 var marchList = regexStyle.Matches(html);
                     if (marchList.Count > 0)
                     {
                         foreach (Match item in marchList)
                         {
                             if (string.IsNullOrWhiteSpace(item.Value))
                             {
                                 continue;
                             }
                             html = html.Replace(item.Value, keyCss);
                         //压缩style
                         string minCss = minifier.MinifyStyleSheet(item.Value, cssSettings, codeSettings);
                             html = html.Replace(keyCss, minCss);
                         }
                     }
                 });
            }
            var reg = new Regex(@"\s*(</?[^\s/>]+[^>]*>)\s+(</?[^\s/>]+[^>]*>)\s*");
            text = reg.Replace(text, m => m.Groups[1].Value + m.Groups[2].Value);
            //移除html标签之间的空格 
            text = new Regex(@"(?<=>)[\s|\n|\t]*(?=<)").Replace(text, string.Empty);
            //移除多余空格和换行符
            text = new Regex(@"\n+\s+").Replace(text, string.Empty);
            return html;
        }

        /// <summary>
        ///     在执行Action的时候,就把需要的Writer存起来
        /// </summary>
        /// <param name="filterContext">上下文</param>
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            _builder = new StringBuilder();
            _writer = new StringWriter(_builder);
            _htmlWriter = new HtmlTextWriter(_writer);
            _output = (HttpWriter)filterContext.RequestContext.HttpContext.Response.Output;
            filterContext.RequestContext.HttpContext.Response.Output = _htmlWriter;
            base.OnActionExecuting(filterContext);
        }

        /// <summary>
        ///在执行完成后,处理得到的HTML,并将他输出到前台
        /// </summary>
        /// <param name="filterContext"></param>
        protected override void OnResultExecuted(ResultExecutedContext filterContext)
        {
            var response = Compress(_builder.ToString(),filterContext);
            filterContext.HttpContext.Response.StatusCode = 200;
            _output.Write(response);
        }
        #endregion

}

 若压缩后的HTML有问题,可根据HTML调整正则。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值