-
最近要做網站靜態化,自己寫了幾個方法,貼出來跟大家討論討論。
/// <summary>
/// 網站靜態化功能類
/// </summary>
public class CreateHtml
{
/// <summary>
/// 讀取範本內容
/// </summary>
/// <param name="template">模板相對路徑</param>
/// <returns>模板內容,讀取失敗返回""</returns>
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
}
/// <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)
{
#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
}
/// <summary>
/// 依照讀取到所有內容進行分頁讀取,每頁與每頁以「|」分割
/// </summary>
/// <param name="contentText">內容</param>
/// <param name="pageLogo">分頁標誌</param>
/// <returns>內容分頁後字串,每頁與每頁以「|」分割</returns>
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
}
/// <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)
{
#region
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;
#endregion
}
/// <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)
{
#region
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();
#endregion