日韩天天综合网_野战两个奶头被亲到高潮_亚洲日韩欧美精品综合_av女人天堂污污污_视频一区**字幕无弹窗_国产亚洲欧美小视频_国内性爱精品在线免费视频_国产一级电影在线播放_日韩欧美内地福利_亚洲一二三不卡片区

ASP.NET生成靜態(tài)HTML頁面并分別按年月目錄存放_.Net教程

編輯Tag賺U幣
教程Tag:暫無Tag,歡迎添加,賺取U幣!

推薦:ASP.Net中利用CSS實現(xiàn)多界面兩法
通過使頁面動態(tài)加載不同CSS實現(xiàn)多界面 (類似于這個blog) 方法一: 以下為引用的內(nèi)容: <%@page language="C#"%> <%@import namespace=&

一說到新聞系統(tǒng)的話,一定會談到靜態(tài)頁面生成的,因為靜態(tài)頁面不但是讀取速度快,而且又安全;

靜態(tài)頁面的生成不管是小到現(xiàn)在的企業(yè)網(wǎng)站大至網(wǎng)易,QQ等門戶都用到了;

那么我們?nèi)绾蝸砩伸o態(tài)頁呢?

以什么方式生成靜態(tài)頁面呢……

在生成靜態(tài)頁面的時候有那些是要注意的呢:

靜態(tài)頁面命名

統(tǒng)一存放目錄

靜態(tài)頁面模板

頁面生成

一般來說,在原來新聞系統(tǒng)的基礎(chǔ)上我們可以根據(jù)GET此頁面請求的內(nèi)容再生成(比如:http;//www.test.com/news.aspx?id=1,GET此頁面代碼直接寫至一個文本文件并以HTML命名即可);

在這里我所采用的是模板生成,先用DW做一個網(wǎng)頁模板,將標(biāo)題,內(nèi)容等將要動態(tài)實現(xiàn)的內(nèi)容先以$Title$等替換,等在生成的時候替換成新聞的內(nèi)容;

命名:在生成的時候一般來說是采用新聞發(fā)布的時間轉(zhuǎn)換成的字符串,這個是不會重復(fù)的。另外我還按年份月份把這些靜態(tài)文件存放在不同的目錄,以便于管理,在這里根據(jù)一個新聞的ID調(diào)用方法WriteNews()給定參數(shù)ID,它就會根據(jù)此ID從數(shù)據(jù)庫中讀取內(nèi)容,再根據(jù)靜態(tài)模板頁面html/test.html生成新的靜態(tài)頁面存放在相應(yīng)年份月份的目錄

好了,下面是代碼:

以下為引用的內(nèi)容:

using System;
using System.IO;
using System.Web;
using System.Text;
namespace PowerLeader.Components
...{
/**//// <summary>
/// WriteTOHtml 的摘要說明。
/// </summary>
public class WriteTOHtml
...{
public WriteTOHtml()
...{
//
// TODO: 在此處添加構(gòu)造函數(shù)邏輯
//
}

public static void WriteNews(int id)
...{
News news = new News();
News.NewsDetails newsDetails = new PowerLeader.Components.News.NewsDetails();
newsDetails = news.GetNews(id);
bool flag;
flag = WriteFile(newsDetails);
}

public static bool WriteFile(News.NewsDetails newsDetails)
...{
Directory.CreateDirectory(HttpContext.Current.Server.MapPath("/PowerLeader/html/" newsDetails.addtime.ToString("yyyy") "/" newsDetails.addtime.ToString("MM")));
string path = HttpContext.Current.Server.MapPath("../html/" newsDetails.addtime.ToString("yyyy") "/" newsDetails.addtime.ToString("MM") "/");
Encoding code = Encoding.GetEncoding("gb2312");
// 讀取模板文件
string temp = HttpContext.Current.Server.MapPath("../html/text.html");
StreamReader sr = null;
StreamWriter sw = null;
string stringTempCode = "";
try
...{
sr = new StreamReader(temp, code);
stringTempCode = sr.ReadToEnd(); // 讀取文件
}
catch(Exception exp)
...{
HttpContext.Current.Response.Write(exp.Message);
HttpContext.Current.Response.End();
sr.Close();
}
string htmlFileName = newsDetails.addtime.ToString("yyyyMMddHHmmss") ".html";
// 替換內(nèi)容
// 這時,模板文件已經(jīng)讀入到名稱為str的變量中了
stringTempCode = stringTempCode.Replace("$PageTitle$","抗戰(zhàn)OnLine官方網(wǎng)站...");
stringTempCode = stringTempCode.Replace("$Type$",newsDetails.type.ToString().Trim());
stringTempCode = stringTempCode.Replace("$Author$",newsDetails.author.ToString().Trim());
stringTempCode = stringTempCode.Replace("$From$",newsDetails.from.Trim());
stringTempCode = stringTempCode.Replace("$Time$",newsDetails.addtime.ToString().Trim());
stringTempCode = stringTempCode.Replace("$Title$",newsDetails.title.Trim());
stringTempCode = stringTempCode.Replace("$Content$",newsDetails.content);
// 寫文件
try
...{
sw = new StreamWriter(path htmlFileName , false, code);
sw.Write(stringTempCode);
sw.Flush();
}
catch(Exception ex)
...{
HttpContext.Current.Response.Write(ex.Message);
HttpContext.Current.Response.End();
}
finally
...{
sw.Close();
}
return true;
}
}
}

分享:ASP.NET 遍歷配置文件的連接字符串
在ASP.NET 2.0中,提供了更方便的配置文件訪問的類,具體可以到 System.Configuration 名稱空間下進(jìn)行查看。本文提供一種在開發(fā)過程中常用的得到數(shù)據(jù)庫字符串的方法,為方便使用,寫成一個方法

來源:模板無憂//所屬分類:.Net教程/更新時間:2008-08-22
相關(guān).Net教程