/// <summary> /// action executing... /// </summary> /// <param name="filterContext"></param> public override void OnActionExecuting(ActionExecutingContext filterContext) { if (RequestHelper.IsTextHtml(filterContext.HttpContext.Request)) { var response = filterContext.HttpContext.Response; var originStream = response.Filter; var buffer = originStream.ToArray(); var compresedData = MinifyHelper.Compress(buffer); originStream.Write(compresedData, 0, compresedData.Length); response.Filter = originStream; } }
/// <summary> /// 合并模板和数据字典 /// </summary> /// <param name="templateName">Velocity模板文件名称</param> /// <param name="dict">Velocity模板文件所用到的数据字典</param> /// <returns>返回Velocity模板页面内容(去掉VM语法之后的结果)</returns> private static string MergeVM(string templateName, Dictionary <string, object> dict) { string html = null; Template template = NVelocityBus._VelocityEngine.GetTemplate(templateName);//模板页名字 VelocityContext velcityContext = new VelocityContext(); foreach (KeyValuePair <string, object> pair in dict) { velcityContext.Put(pair.Key, pair.Value);//填充数据,在模板中可以通过$data来引用 } using (System.IO.StringWriter stringWriter = new System.IO.StringWriter()) { template.Merge(velcityContext, stringWriter);//将数据和模板内容进行合并 html = stringWriter.GetStringBuilder().ToString(); } velcityContext = null; template = null; html = MinifyHelper.MinifyHtml(html); return(html); }