Пример #1
0
        protected string RequireTemplate(string content)
        {
            string html     = null;
            var    response = Response;

            var _tpl = new MicroTemplateEngine(tpl);

            html = _tpl.Execute(content);

            object value;

            foreach (var p in ViewData.Keys)
            {
                if ((value = ViewData[p]) != null)
                {
                    html = ReplaceHtml(html, p.ToString(), value.ToString());
                }
            }

            var query = Request.GetQueryString();

            if (!Array.Exists(ignoreURI, a => query.IndexOf(a, StringComparison.Ordinal) != -1))
            {
                html = CompressHtml(html);
            }

            // if (Settings.Opti_SupportGZip)
            // {
            //     response.Filter = new GZipStream(response.Filter, CompressionMode.Compress);
            //     response.AddHeader("Content-Encoding", "gzip");
            // }

            HttpHosting.Context.TryGetItem <string>("ajax", out var isAjax);

            if (Request.Query("ajax") == "1" || isAjax == "1")
            {
                const string ajaxPattern = "<body([^>]*)>([\\s\\S]+)</body>";
                if (Regex.IsMatch(html, ajaxPattern))
                {
                    var match = Regex.Match(html, ajaxPattern);

                    return(match.Groups[2].Value);
                }
            }

            return(html);
        }
Пример #2
0
        public static void SaveErrorLog(Exception exception)
        {
            lock (ErrorFilePath)
            {
                ICompatibleRequest req = HttpCtx.Request;
                var path         = req.GetPath();
                var query        = req.GetQueryString();
                var PathAndQuery = path + query;
                var referer      = req.GetHeader("Referer");;
                if (!File.Exists(ErrorFilePath))
                {
                    string dir = EnvUtil.GetBaseDirectory() + "tmp/logs";
                    if (!Directory.Exists(dir))
                    {
                        Directory.CreateDirectory(dir);
                    }
                    File.Create(ErrorFilePath).Close();
                }

                HttpCtx.Response.WriteAsync((File.Exists(ErrorFilePath).ToString()));
                using (FileStream fs = new FileStream(ErrorFilePath, FileMode.Append, FileAccess.Write))
                {
                    StreamWriter  sw = new StreamWriter(fs);
                    StringBuilder sb = new StringBuilder();

                    sb.Append("---------------------------------------------------------------------\r\n")
                    .Append("[错误]:IP:").Append(HttpCtx.RemoteAddress())
                    .Append("\t时间:").Append(DateTime.Now.ToString())
                    .Append("\r\n[信息]:").Append(exception.Message)
                    .Append("\r\n[路径]:").Append(PathAndQuery)
                    .Append("  -> 来源:").Append(referer)
                    .Append("\r\n[堆栈]:").Append(exception.StackTrace)
                    .Append("\r\n\r\n");

                    sw.Write(sb.ToString());

                    sw.Flush();
                    sw.Dispose();
                    fs.Dispose();
                }
            }
        }