Пример #1
0
        /// <summary>
        /// 生成引入CSS文件的HTML代码
        /// </summary>
        /// <param name="path"></param>
        /// <param name="inline"></param>
        /// <returns></returns>
        public static string RefCssFileHtml(string path, bool inline)
        {
            string filePath = AppRoot + path.Replace("/", "\\");

            if (inline)
            {
                return(string.Format("<style type=\"text/css\">\r\n{0}\r\n</style>",
                                     RetryFile.ReadAllText(filePath, Encoding.UTF8)));
            }
            else
            {
                string version = RetryFile.GetLastWriteTimeUtc(filePath).Ticks.ToString();
                return(string.Format("<link type=\"text/css\" rel=\"Stylesheet\" href=\"{0}?_t={1}\" />", path, version));
            }
        }
Пример #2
0
        /// <summary>
        /// 生成引入JavaScript文件的HTML代码
        /// </summary>
        /// <param name="path"></param>
        /// <param name="inline"></param>
        /// <returns></returns>
        public static string RefJsFileHtml(string path, bool inline)
        {
            string filePath = AppRoot + path.Replace("/", "\\");

            if (inline)
            {
                return(string.Format("<script type=\"text/javascript\">\r\n{0}\r\n</script>",
                                     RetryFile.ReadAllText(filePath, Encoding.UTF8)));
            }
            else
            {
                string version = RetryFile.GetLastWriteTimeUtc(filePath).Ticks.ToString();
                return(string.Format("<script type=\"text/javascript\" src=\"{0}?_t={1}\"></script>", path, version));
            }
        }
Пример #3
0
        private void OutputCssFile(HttpContext context)
        {
            // 1. 先读出文件内容。注意这里使用UTF-8编码
            // 2. 用正则表达式搜索所有的引用文件
            // 3. 循环匹配结果,
            // 4. 对于匹配之外的内容,直接写入StringBuilder实例,
            // 5. 如果是文件,则计算版本号,再一起写入到StringBuilder实例
            // 6. 最后,StringBuilder实例包含的内容就是处理后的结果。

            string text = RetryFile.ReadAllText(context.Request.PhysicalPath, Encoding.UTF8);

            MatchCollection matches = s_CssBackgroundImageRegex.Matches(text);

            if (matches != null && matches.Count > 0)
            {
                int           lastIndex = 0;
                StringBuilder sb        = new StringBuilder(text.Length * 2);

                foreach (Match m in matches)
                {
                    Group g = m.Groups["file"];
                    if (g.Success)
                    {
                        sb.Append(text.Substring(lastIndex, g.Index - lastIndex + g.Length));
                        lastIndex = g.Index + g.Length;

                        //string fileFullPath = HttpRuntime.AppDomainAppPath.TrimEnd('\\') + g.Value.Replace("/", "\\");
                        string fileFullPath = WebRuntime.Instance.GetPhysicalPath(g.Value.Replace("/", "\\"));
                        if (RetryFile.Exists(fileFullPath))
                        {
                            string version = RetryFile.GetLastWriteTimeUtc(fileFullPath).Ticks.ToString();
                            sb.Append("?_v=").Append(version);
                        }
                    }
                }

                if (lastIndex > 0 && lastIndex < text.Length)
                {
                    sb.Append(text.Substring(lastIndex));
                }

                context.Response.Write(sb.ToString());
            }
            else
            {
                context.Response.Write(text);
            }
        }