asp.net网站静态化

作者:互联网

2009-11-30

ASP.NET教程

-
最近要做网站静态化,自己写了几个方法,贴出来和大家讨论讨论。

///


    /// 网站静态化功能类
    ///

    public class CreateHtml
    {
        ///
        /// 读取模板内容
        ///

        /// 模板相对路径
        /// 模板内容,读取失败返回""
        public string Html_ReadTemplate(string template)
        {
            #region
            StringBuilder str = new StringBuilder();//存放模式内容
            if (template != null && template != "" && template.Length > 0)//如果
            {
                try
                {
                    using (StreamReader sr = new StreamReader(System.Web.HttpContext.Current.Server.MapPath(template), Encoding.GetEncoding("utf-8")))
                    {
                        string tempStr;
                        while ((tempStr = sr.ReadLine()) != null)//如果没有读取文件末尾
                        {
                            str.Append(tempStr);
                        }
                        sr.Close();
                    }
                }
                catch (Exception ex)
                {
                    return null;                   
                }
            }
            return str.ToString();
            #endregion
        }
        ///
        /// 根据生成文件绝对路径、生成文件名生成文件
        ///

        /// 文件绝对路径
        /// 生成文件名
        /// 写入文件内容
        /// 生成文件状态,1:成功 0:失败
        public int Html_WriteTemplate(string fileAbsolutePath, string htmlName,string htmlNote)
        {
            #region
            if (fileAbsolutePath != null && fileAbsolutePath != "" && fileAbsolutePath.Length > 0 && htmlName != null && htmlName != "" && htmlName.Length > 0)
            {
                try
                {
                    using (FileStream fs = new FileStream(fileAbsolutePath + "\" + htmlName, FileMode.Create, FileAccess.Write, FileShare.Write))
                    {
                        StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.GetEncoding("utf-8"));
                        sw.Write(htmlNote);
                        sw.Close();
                        fs.Close();
                        return 1;//文件生成成功
                    }
                }
                catch (Exception ex)
                {
                    return 0;
                }
            }
            return 0;//文件生成失败
            #endregion
        }
        ///
        /// 根据读取到所有内容进行分页读取,每页与每页用“|”分割
        ///

        /// 内容
        /// 分页标志
        /// 内容分页后字符串,每页与每页用“|”分割
        public string Html_BackPageData(string contentText,string pageLogo)
        {
            #region
            int tempcount = 0;
            StringBuilder sb = new StringBuilder();
            while (tempcount >= 0)
            {
                int index = contentText.IndexOf(pageLogo);//分页标志
                if (index == -1)//如果没有分页了
                {
                    sb.Append(contentText);
                    break;
                }
                else
                {
                    sb.Append(contentText.Substring(0,index)+"|");
                    int start = index + pageLogo.Length + 1;
                    int end = contentText.Length-index-pageLogo.Length - 1;
                    contentText=contentText.Substring(start, end);
                }
                tempcount++;
            }
            return sb.ToString();
            #endregion
        }
        ///
        /// 针对内容生成相应的分页标志,首页、上一页、页码、下一页、尾页(针对一条内容)
        ///

        /// 当前页索引
        /// 总页数
        /// 文件名(只跟文件名不跟后缀名)
        /// 根据内容生成的相应分页标志
        public string Html_Pager_Sign(int currPageIndex, int pageCount, string noteName)
        {
            #region
            string allSeprNote = "
   $firstpage$  $uppage$ $pagenumber$  $drownpage$  $lastpage$
";
            string seprTempStr = "";
            for (int i = 1; i <= pageCount; i++)//循环生成每页页码(本循环只生成页码)
            {
                int n = i - 1;
                //如果是第一页
                if (i == 1)
                {
                    seprTempStr += "" + i+" "+ "";
                }
                else
                {
                    seprTempStr += "" + i+" " + "";
                }
            }
            allSeprNote = allSeprNote.Replace("$pagenumber$", seprTempStr);//替换页码
            //生成首页、上一页
            if (currPageIndex == 0)//如果当前页为第一页,则就没有上一页,和首页的链接
            {
                allSeprNote = allSeprNote.Replace("$firstpage$", "首页");//替换首页
                allSeprNote = allSeprNote.Replace("$uppage$", "上一页");//替换上一页
            }
            else if (currPageIndex == 1)//如果当前页是第二页,就有上一页和首页链接,并且上一页与首页链接是一样的,也就是功能一样
            {
                allSeprNote = allSeprNote.Replace("$firstpage$", "首页");//替换首页,有链接
                allSeprNote = allSeprNote.Replace("$uppage$", "上一页");//替换上一页,因为是第二页所以链接与首页相同
            }
            else//如果是其他页
            {
                int temppageindex = currPageIndex - 1;//上一页页码
                allSeprNote = allSeprNote.Replace("$firstpage$", "首页");//替换首页
                allSeprNote = allSeprNote.Replace("$uppage$", "上一页");//替换上一页
            }
            //生成尾页、下一页
            if (currPageIndex == pageCount - 1)//如果当前页为最后一页,则下一页与尾页功能相同,而且都无链接
            {
                allSeprNote = allSeprNote.Replace("$lastpage$", "尾页");//替换尾页
                allSeprNote = allSeprNote.Replace("$drownpage$", "下一页");//替换下一页
            }
            else//如果是其他页
            {
                int temppageindex = currPageIndex+1;//下一页页码
                allSeprNote=allSeprNote.Replace("$lastpage$", "尾页");//生成尾页
                allSeprNote=allSeprNote.Replace("$drownpage$", "下一页");
            }
            return allSeprNote;
            #endregion
        }
        ///
        /// 针对列表页生成分页标志
        ///

        /// 总页数
        /// 当前页索引(从零开始)
        /// 每页显示内容条数
        /// 文件名(不带后缀)
        /// 分页标志
        public string Html_Pager_Multi(int pageTotal, int currentPage, int pageSize, string name,string path)
        {
            #region
            pageTotal = pageTotal - 1;
            StringBuilder sb = new StringBuilder();
            //生成首页、上一页链接
            if (currentPage == 0)//如果当前页是第一页
            {
                sb.Append("  首页  上一页  ");
            }
            else if (currentPage == 1)//如果当前页是第二页
            {
                sb.Append("  首页");
                sb.Append("  上一页  ");
            }
            else
            {
                sb.Append("  首页");
                sb.Append("  上一页  ");
            }
            int indexStart = currentPage - 5;
            int indexEnd = currentPage + 5;
            if (indexStart <= 0)
            {
                indexStart = 0;
                indexEnd = 10;
            }
            if (indexStart > 0)
            {
                indexStart = currentPage - 5;
                indexEnd = currentPage + 5;
            }
            if (currentPage >= pageTotal-10)
            {
                indexStart = pageTotal - 10;
                indexEnd = pageTotal;
            }
            //生成详细页码
            for (int i = indexStart; i <= indexEnd; i++)
            {

                if (i == currentPage)
                {
                    sb.Append(" " + (i+1) + " ");
                }
                else if (i == 0)//如果当前页是第二页,需要单独处理一下
                {
                    sb.Append(" " + (i + 1) + " ");
                }
                else
                {
                    sb.Append(" " + (i + 1) + " ");
                }
            }
            //生成下一页、尾页
            if (currentPage == pageTotal)//如果当前页是最后一页
            {
                sb.Append("  下一页  ");
                sb.Append("  尾页  ");
            }
            else
            {
                sb.Append("  下一页  ");
                sb.Append("  尾页");
            }
            sb.Append("    " + (currentPage + 1) + "/" + (pageTotal + 1));
            sb.Append("    转到:");
            sb.Append("");
            return sb.ToString();
            #endregion