public override void Write(byte[] buffer, int offset, int count) { if (!context.Response.StatusCode.Equals(200)) { return; } inner.Write(buffer, offset, count); //if (!context.HttpContext.Request.FilePath.ToLower().StartsWith("/admin")) //当前请求不是后台管理;并且返回客户端是html才生成静态页面 if (!context.Request.FilePath.ToLower().StartsWith("/admin") && context.Response.ContentType.Equals("text/html")) { //静态页面保存路径信息 string htmlPath = HtmlPageHelper.GetHtmlPathFromUrl(context.Request.Url.AbsoluteUri); string direcHtmlPath = Path.GetDirectoryName(htmlPath); if (!Directory.Exists(direcHtmlPath)) { Directory.CreateDirectory(direcHtmlPath); } //获取返回客户端的html代码,并进行压缩处理 string htmlCode = System.Text.Encoding.UTF8.GetString(buffer); string isZipHtml = System.Configuration.ConfigurationManager.AppSettings["IsCompressed"]; //如果“IsCompressed”的值为空或0,则表示不压缩 if (!string.IsNullOrEmpty(isZipHtml) && !isZipHtml.Equals(0)) { htmlCode = Regex.Replace(htmlCode, "^\\s*", string.Empty, RegexOptions.Compiled | RegexOptions.Multiline); htmlCode = Regex.Replace(htmlCode, "\\r\\n", string.Empty, RegexOptions.Compiled | RegexOptions.Multiline); htmlCode = Regex.Replace(htmlCode, "<!--*.*?-->", string.Empty, RegexOptions.Compiled | RegexOptions.Multiline); } //保存文件,这里不能用File.WriteAllText File.AppendAllText(htmlPath, htmlCode); } }
private void Application_BeginRequest(Object source, EventArgs e) { HttpApplication application = (HttpApplication)source; HttpContext context = application.Context; string filePath = context.Request.FilePath; string fileExtension = VirtualPathUtility.GetExtension(filePath); bool isFile = !string.IsNullOrEmpty(fileExtension); //如果当前请求的不是资源文件、不是后台管理、请求中没有表单信息、静态页面存在,则返回静态页面 if ((!isFile || fileExtension == ".zeje") && !filePath.ToLower().StartsWith("/admin") && context.Request.Form.Count.Equals(0)) { string htmlPath = HtmlPageHelper.GetHtmlPathFromUrl(context.Request.Url.AbsoluteUri); if (File.Exists(htmlPath)) { context.Response.WriteFile(htmlPath); context.Response.End(); } else { context.Response.Filter = new HttpResponseFilterWrapper(context.Response.Filter, context); } } }