最近要做网站静态化,自己写了几个方法,贴出来和大家讨论讨论。
/// <summary> | |
/// 网站静态化功能类 | |
/// </summary> | |
public class CreateHtml | |
{ | |
/// <summary> | |
/// 读取模板内容 | |
/// </summary> | |
/// <param name="template">模板相对路径</param> | |
/// <returns>模板内容,读取失败返回""</returns> | |
public string Html_ReadTemplate(string template) | |
{ | |
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(); | |
} | |
/// <summary> | |
/// 根据生成文件绝对路径、生成文件名生成文件 | |
/// </summary> | |
/// <param name="fileAbsolutePath">文件绝对路径</param> | |
/// <param name="htmlName">生成文件名</param> | |
/// <param name="htmlNote">写入文件内容</param> | |
/// <returns>生成文件状态,1:成功 0:失败</returns> | |
public int Html_WriteTemplate(string fileAbsolutePath, string htmlName,string htmlNote) | |
{ | |
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;//文件生成失败 | |
} | |
/// <summary> | |
/// 根据读取到所有内容进行分页读取,每页与每页用“|”分割 | |
/// </summary> | |
/// <param name="contentText">内容</param> | |
/// <param name="pageLogo">分页标志</param> | |
/// <returns>内容分页后字符串,每页与每页用“|”分割</returns> | |
public string Html_BackPageData(string contentText,string pageLogo) | |
{ | |
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(); | |
} | |
/// <summary> | |
/// 针对内容生成相应的分页标志,首页、上一页、页码、下一页、尾页(针对一条内容) | |
/// </summary> | |
/// <param name="currPageIndex">当前页索引</param> | |
/// <param name="pageCount">总页数</param> | |
/// <param name="noteName">文件名(只跟文件名不跟后缀名)</param> | |
/// <returns>根据内容生成的相应分页标志</returns> | |
public string Html_Pager_Sign(int currPageIndex, int pageCount, string noteName) | |
{ | |
string allSePRNote = "<table><tr><td> $firstpage$</td><td> $uppage$</td><td> $pagenumber$</td><td> $drownpage$</td><td> $lastpage$</td></tr></table>"; | |
string seprTempStr = ""; | |
for (int i = 1; i <= pageCount; i++)//循环生成每页页码(本循环只生成页码) | |
{ | |
int n = i - 1; | |
//如果是第一页 | |
if (i == 1) | |
{ | |
seprTempStr += "<a href='" + noteName + ".html'>" + i+" "+ "</a>"; | |
} | |
else | |
{ | |
seprTempStr += "<a href='" + noteName +"-"+n+ ".html'>" + i+" " + "</a>"; | |
} | |
} | |
allSeprNote = allSeprNote.Replace("$pagenumber$", seprTempStr);//替换页码 | |
//生成首页、上一页 | |
if (currPageIndex == 0)//如果当前页为第一页,则就没有上一页,和首页的链接 | |
{ | |
allSeprNote = allSeprNote.Replace("$firstpage$", "首页");//替换首页 | |
allSeprNote = allSeprNote.Replace("$uppage$", "上一页");//替换上一页 | |
} | |
else if (currPageIndex == 1)//如果当前页是第二页,就有上一页和首页链接,并且上一页与首页链接是一样的,也就是功能一样 | |
{ | |
allSeprNote = allSeprNote.Replace("$firstpage$", "<a href='" + noteName + ".html'>首页</a>");//替换首页,有链接 | |
allSeprNote = allSeprNote.Replace("$uppage$", "<a href='" + noteName + ".html'>上一页</a>");//替换上一页,因为是第二页所以链接与首页相同 | |
} | |
else//如果是其他页 | |
{ | |
int temppageindex = currPageIndex - 1;//上一页页码 | |
allSeprNote = allSeprNote.Replace("$firstpage$", "<a href='" + noteName + ".html'>首页</a>");//替换首页 | |
allSeprNote = allSeprNote.Replace("$uppage$", "<a href='" + noteName +"-"+temppageindex+ ".html'>上一页</a>");//替换上一页 | |
} | |
//生成尾页、下一页 | |
if (currPageIndex == pageCount - 1)//如果当前页为最后一页,则下一页与尾页功能相同,而且都无链接 | |
{ | |
allSeprNote = allSeprNote.Replace("$lastpage$", "尾页");//替换尾页 | |
allSeprNote = allSeprNote.Replace("$drownpage$", "下一页");//替换下一页 | |
} | |
else//如果是其他页 | |
{ | |
int temppageindex = currPageIndex+1;//下一页页码 | |
allSeprNote=allSeprNote.Replace("$lastpage$", "<a href='" + noteName + "-" + (pageCount - 1) + ".html'>尾页</a>");//生成尾页 | |
allSeprNote=allSeprNote.Replace("$drownpage$", "<a href='" + noteName + "-" + temppageindex + ".html'>下一页</a>"); | |
} | |
return allSeprNote; | |
} | |
/// <summary> | |
/// 针对列表页生成分页标志 | |
/// </summary> | |
/// <param name="pageTotal">总页数</param> | |
/// <param name="currentPage">当前页索引(从零开始)</param> | |
/// <param name="pageSize">每页显示内容条数</param> | |
/// <param name="name">文件名(不带后缀)</param> | |
/// <returns>分页标志</returns> | |
public string Html_Pager_Multi(int pageTotal, int currentPage, int pageSize, string name,string path) | |
{ | |
pageTotal = pageTotal - 1; | |
StringBuilder sb = new StringBuilder(); | |
//生成首页、上一页链接 | |
if (currentPage == 0)//如果当前页是第一页 | |
{ | |
sb.Append(" 首页 上一页 "); | |
} | |
else if (currentPage == 1)//如果当前页是第二页 | |
{ | |
sb.Append(" <a href='" + name + ".html" + "'>首页</a>"); | |
sb.Append(" <a href='" + name + ".html" + "'>上一页</a> "); | |
} | |
else | |
{ | |
sb.Append(" <a href='" + name + ".html" + "'>首页</a>"); | |
sb.Append(" <a href='" + name + "-" + (currentPage - 1) + ".html'>上一页</a> "); | |
} | |
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(" <a href='" + name + ".html'>" + (i + 1) + "</a> "); | |
} | |
else | |
{ | |
sb.Append(" <a href='" + name + "-" + i + ".html'>" + (i + 1) + "</a> "); | |
} | |
} | |
//生成下一页、尾页 | |
if (currentPage == pageTotal)//如果当前页是最后一页 | |
{ | |
sb.Append(" 下一页 "); | |
sb.Append(" 尾页 "); | |
} | |
else | |
{ | |
sb.Append(" <a href='"+name+"-"+(currentPage+1)+".html'>下一页</a> "); | |
sb.Append(" <a href='"+name+"-"+pageTotal+".html'>尾页</a>"); | |
} | |
sb.Append(" " + (currentPage + 1) + "/" + (pageTotal + 1)); | |
sb.Append(" 转到:<select name=\"QQ\" id=\"ddlIndexList\" onchange=\"javascript:location.href=this.value;\">"); | |
string absolutePaths = HttpContext.Current.Request.Url.ToString();//获取url | |
absolutePaths = absolutePaths.Substring(0, absolutePaths.LastIndexOf("/"))+"\\" + path;//获取网站根路径url | |
for (int j = 1; j <= (pageTotal+1); j++) | |
{ | |
if (j == 1) | |
{ | |
sb.Append("<option value='" + absolutePaths+"\\"+name + ".html'>" + j + "</option>"); | |
} | |
else | |
{ | |
sb.Append("<option value='" + absolutePaths+"\\"+name + "-" + (j - 1) + ".html'>" + j + "</option>"); | |
} | |
} | |
sb.Append("</select>"); | |
sb.Append("<script type=\"text/Javascript\" language=\"javascript\">"); | |
if (currentPage == 0)//第一页 | |
{ | |
string d = "document.getElementById(\"ddlIndexList\").value = \"" + name + ".html\";"; | |
sb.Append("document.getElementById(\"ddlIndexList\").value = \""+name+".html\";"); | |
} | |
else | |
{ | |
string k = " document.getElementById(\"ddlIndexList\").value = \"" + name + "-" + currentPage + ".html\";"; | |
sb.Append(" document.getElementById(\"ddlIndexList\").value = \"" + name+"-"+currentPage + ".html\";"); | |
} | |
sb.Append("</script>"); | |
return sb.ToString(); | |
} | |
} |