asp.net產生靜態頁

來源:互聯網
上載者:User

 

 

 

做個產生靜態頁樣本:

 

採用替換模版頁的形式產生靜態頁

 

第一步:建立項目,建立一個簡單模版頁:TemplatePage.htm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Porschev 產生靜態頁簡單樣本</title>
</head>
<body>
<h1>$Porschev[0]$</h1>
<ul>
<li>網頁標題:$Porschev[0]$</li>
<li>名稱:$Porschev[1]$</li>
<li>網址:<a href="$Porschev[2]$" target="_blank">$Porschev[2]$</a></li>
<li>時間:$Porschev[3]$</li>
<li>詳述:$Porschev[4]$</li>
</ul>
</body>
</html>

 

第二步:建立一個config檔案:CreateHtml.config

<?xml version="1.0" encoding="utf-8" ?>
<web>
<website key="0" value="title"/>
<website key="1" value="name"/>
<website key="2" value="url"/>
<website key="3" value="createDate"/>
<website key="4" value="desc"/>
</web>

 

第三步:編寫產生靜態頁代碼:(添加System.Web引用)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
namespace CreateHtmlBLL
{
public class CreateHtmlBLL
{
#region##讀取設定檔某節點的個數
///<summary>
/// 讀取設定檔某節點的個數
///</summary>
///<param name="path">設定檔的路徑</param>
///<param name="nodeName">要擷取的節點</param>
///<returns>返回節點個數</returns>
private int ReadConfig(string path,string nodeName)
{
string absoPath = string.Empty; //絕對路徑
try
{
absoPath = System.Web.HttpContext.Current.Server.MapPath(path);
XmlDocument xd = new XmlDocument();
xd.Load(absoPath);
XmlNodeList nodeList = xd.SelectNodes(nodeName); //得到相應節點的集合
return nodeList.Count;
}
catch (Exception)
{
throw;
}
}
#endregion
#region##建立檔案夾
///<summary>
/// 建立檔案夾
///</summary>
///<param name="path">要建立的路徑</param>
public void CreatFolder(string path)
{
string absoPath = string.Empty; //絕對路徑
try
{
absoPath = System.Web.HttpContext.Current.Server.MapPath(path);
if (!Directory.Exists(absoPath))
{
Directory.CreateDirectory(absoPath);
}
}
catch (Exception)
{

throw;
}
}
#endregion

#region##產生HTML頁
///<summary>
/// 產生HTML頁
///</summary>
///<param name="configPath">設定檔的路徑</param>
///<param name="configNodeName">設定檔節點名</param>
///<param name="temPath">模版頁路徑</param>
///<param name="arr">替換數組</param>
///<param name="createPath">產生HTML路徑</param>
public void CreateHtml(string configPath, String configNodeName, string temPath, string[] arr,string createPath)
{
string fileName = string.Empty; //組建檔案名
string absoCrePath = string.Empty; //產生頁絕對路徑
string absoTemPath = string.Empty; //模版頁的絕對中徑
int nodeCount = 0; //節點數

try
{
absoCrePath = System.Web.HttpContext.Current.Server.MapPath(createPath);
absoTemPath = System.Web.HttpContext.Current.Server.MapPath(temPath);
nodeCount = ReadConfig(configPath, configNodeName);
FileStream fs = File.Open(absoTemPath, FileMode.Open, FileAccess.Read); //讀模數版頁
StreamReader sr = new StreamReader(fs, Encoding.GetEncoding("utf-8"));
StringBuilder sb = new StringBuilder(sr.ReadToEnd());
sr.Close();
sr.Dispose();
for (int i = 0; i < nodeCount; i++)
{
sb.Replace("$Porschev[" + i + "]$", arr[i]);
}
CreatFolder(createPath);
fileName = DateTime.Now.ToFileTime().ToString() + ".html"; //設定檔案名稱(這裡可以根據需要變化命名)
FileStream cfs = File.Create(absoCrePath + "/" + fileName);
StreamWriter sw = new StreamWriter(cfs, Encoding.GetEncoding("utf-8"));
sw.Write(sb.ToString());
sw.Flush();
sw.Close();
sw.Dispose();
}
catch (Exception)
{

throw;
}
}
#endregion
}
}

 

第四步:測式產生

 protected void Page_Load(object sender, EventArgs e)
{
CreateHtml();
}
#region##產生靜態頁
///<summary>
/// 產生靜態頁
///</summary>
public void CreateHtml()
{
try
{
string[] arr = new string[5];
arr[0] = "Porschev 靜態頁測式";
arr[1] = "dtan";
arr[2] = "www.dtan.so";
arr[3] = DateTime.Today.ToString();
arr[4] = "上班時間上CSDN,都是不好好工作的閑人。。。";
CreateHtmlBLL.CreateHtmlBLL chb = new CreateHtmlBLL.CreateHtmlBLL();
chb.CreateHtml("CreateHtml.config", "web/website","Template/TemplatePage.htm", arr,"Porschev");
}
catch (Exception ex)
{
throw ex;
}
}
#endregion

 

產生頁面:

 

 

 

樣本源碼下載:http://download.csdn.net/source/3293015
樣本源碼下載二:http://files.cnblogs.com/zhongweiv/CreateHtml.rar

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.