ASP.NET生成靜態(tài)網(wǎng)頁的方法_.Net教程
推薦:如何以及為何創(chuàng)建Search .NET版Search 開發(fā)負(fù)責(zé)人 Larry Jordan、開發(fā)人員 Michael Ruggiero 和 Michael Stanton 以及 .NET 框架項(xiàng)目經(jīng)理 Hari Sekhar 在暗中構(gòu)建了基于 .NET 技術(shù)的 Microsoft Web 站點(diǎn)搜索引擎新版
環(huán)境:Microsoft .NET Framework SDK v1.1
OS:Windows Server 2003 中文版
ASP.Net生成靜態(tài)HTML頁,在Asp中實(shí)現(xiàn)的生成靜態(tài)頁用到的FileSystemObject對(duì)象,在.Net中涉及此類操作的是System.IO
注:以下是參考別人的程序代碼,非原創(chuàng)!
Code:
以下為引用的內(nèi)容: string htmlfilename=DateTime.Now.ToString("yyyyMMddHHmmss") ".html"; 此函數(shù)放在Conn.CS基類中了 if(Hover.Conn.WriteFilethis.Title.Text.ToString),this.Content.Text.ToString),this.Author.Text.ToString)))
/生成HTML頁
public static bool WriteFile(string strText,string strContent,string strAuthor)
{
string path = HttpContext.Current.Server.MapPath("/news/");
Encoding code = Encoding.GetEncoding("gb2312");
// 讀取模板文件
string temp = HttpContext.Current.Server.MapPath("/news/text.html");
StreamReader sr=null;
StreamWriter sw=null;
string str="";
try
{
sr = new StreamReader(temp, code);
str = sr.ReadToEnd(); // 讀取文件
}
catch(Exception exp)
{
HttpContext.Current.Response.Write(exp.Message);
HttpContext.Current.Response.End();
sr.Close();
}
// 替換內(nèi)容
// 這時(shí),模板文件已經(jīng)讀入到名稱為str的變量中了
str =str.Replace("ShowArticle",strText); //模板頁中的ShowArticle
str = str.Replace("biaoti",strText);
str = str.Replace("content",strContent);
str = str.Replace("author",strAuthor);
// 寫文件
try
{
sw = new StreamWriter(path htmlfilename , false, code);
sw.Write(str);
sw.Flush();
}
catch(Exception ex)
{
HttpContext.Current.Response.Write(ex.Message);
HttpContext.Current.Response.End();
}
finally
{
sw.Close();
}
return true;
在添加新聞的代碼中引用 注:工程名為Hover
{
Response.Write("添加成功");
}
else
{
Response.Write("生成HTML出錯(cuò)!");
}
模板頁Text.html代碼
Code:
| 以下為引用的內(nèi)容: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > <HTML> <HEAD> <title>ShowArticle</title> <body> biaoti <br> content<br> author </body> </HTML> biaoti <br> content<br> author </body> </HTML> |
提示添加成功后會(huì)出以當(dāng)前時(shí)間為文件名的html文件!上面只是把傳遞過來的幾個(gè)參數(shù)直接寫入了HTML文件中,在實(shí)際應(yīng)用中需要先添加數(shù)據(jù)庫,然后再寫入HTML文件
請(qǐng)作者聯(lián)系本站,及時(shí)附注您的姓名。聯(lián)系郵箱:mb5u#vip.qq.com(把#改為@)。
分享:ASP.NET網(wǎng)絡(luò)編程中常用到的27個(gè)函數(shù)集1、DateTime 數(shù)字型 以下為引用的內(nèi)容: System.DateTime currentTime=new System.DateTime(); 1.1 取當(dāng)前年月日時(shí)分秒 currentTime=System.DateTime.
- asp.net如何得到GRIDVIEW中某行某列值的方法
- .net SMTP發(fā)送Email實(shí)例(可帶附件)
- js實(shí)現(xiàn)廣告漂浮效果的小例子
- asp.net Repeater 數(shù)據(jù)綁定的具體實(shí)現(xiàn)
- Asp.Net 無刷新文件上傳并顯示進(jìn)度條的實(shí)現(xiàn)方法及思路
- Asp.net獲取客戶端IP常見代碼存在的偽造IP問題探討
- VS2010 水晶報(bào)表的使用方法
- ASP.NET中操作SQL數(shù)據(jù)庫(連接字符串的配置及獲取)
- asp.net頁面?zhèn)髦禍y(cè)試實(shí)例代碼
- DataGridView - DataGridViewCheckBoxCell的使用介紹
- asp.net中javascript的引用(直接引入和間接引入)
- 三層+存儲(chǔ)過程實(shí)現(xiàn)分頁示例代碼
- 相關(guān)鏈接:
- 教程說明:
.Net教程-ASP.NET生成靜態(tài)網(wǎng)頁的方法
。