ASP.NET動(dòng)態(tài)生成靜態(tài)頁(yè)面的實(shí)例代碼
更新時(shí)間:2013年07月26日 11:00:42 作者:
生成靜態(tài)頁(yè)有很多好處,可以緩解服務(wù)器壓力、方便搜索網(wǎng)站搜索等等,下面介紹一下生成靜態(tài)頁(yè)的實(shí)例代碼,有需要的朋友可以參考一下
最近突然想把項(xiàng)目中新聞管理模塊做成靜態(tài)頁(yè),在網(wǎng)上找到很多很好的文章,在這里記錄一下,現(xiàn)在只是實(shí)現(xiàn)靜態(tài)頁(yè)面的生成并沒(méi)有實(shí)現(xiàn)分頁(yè)功能。其主要原理就是讀取數(shù)據(jù)庫(kù)的數(shù)據(jù)然后替換掉靜態(tài)模板頁(yè)的內(nèi)容。
首先制作一個(gè)模板頁(yè),暫時(shí)命名為template.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></title>
</head>
<body>
<div>
$content$
</div>
</body>
</html>
然后制作制作一個(gè)動(dòng)態(tài)頁(yè)面,在這里我們通過(guò)一個(gè)按鈕點(diǎn)擊事件來(lái)生成靜態(tài)頁(yè)面。
前臺(tái)頁(yè)面主要代碼(Default.aspx):
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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 runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtContent" runat="server" Height="179px" TextMode="MultiLine" Width="350px"></asp:TextBox><br />
<br />
<asp:Button ID="btnMake" runat="server" OnClick="btnMake_Click" Text="生成靜態(tài)頁(yè)" />
</div>
</form>
</body>
</html>
后臺(tái)頁(yè)面主要代碼(Default.aspx.cs):
protected void btnMake_Click(object sender, EventArgs e)
{
//替換掉模板中的特征字符
string mbPath = Server.MapPath("template.htm");
Encoding code = Encoding.GetEncoding("UTF-8");
StreamReader sr = null;
StreamWriter sw = null;
string str = null;
//讀取
try
{
sr = new StreamReader(mbPath, code);
str = sr.ReadToEnd();
}
catch (Exception ex)
{
throw ex;
}
finally
{
sr.Close();
}
//根據(jù)時(shí)間自動(dòng)重命名,擴(kuò)展名也可以自行修改
string fileName = DateTime.Now.ToString("yyyyMMddHHmm") + ".htm";
str = str.Replace("$content$", txtContent.Text);//替換content
//生成靜態(tài)文件
try
{
sw = new StreamWriter(Server.MapPath("~/") + fileName, false, code);
sw.Write(str);
sw.Flush();
}
catch (Exception ex)
{
throw ex;
}
finally
{
sw.Close();
Response.Write("<a href=" + fileName + " mce_href=" + fileName + " target=_blank>" + fileName + "</a>已經(jīng)生成!");
}
}
當(dāng)新聞量很大時(shí)這樣做勢(shì)必會(huì)增加服務(wù)器的存儲(chǔ)壓力,暫時(shí)記錄下來(lái)等畢業(yè)設(shè)計(jì)時(shí)再考慮增加動(dòng)態(tài)生成靜態(tài)頁(yè)面,靜態(tài)頁(yè)面分頁(yè)的功能。
首先制作一個(gè)模板頁(yè),暫時(shí)命名為template.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></title>
</head>
<body>
<div>
$content$
</div>
</body>
</html>
然后制作制作一個(gè)動(dòng)態(tài)頁(yè)面,在這里我們通過(guò)一個(gè)按鈕點(diǎn)擊事件來(lái)生成靜態(tài)頁(yè)面。
前臺(tái)頁(yè)面主要代碼(Default.aspx):
復(fù)制代碼 代碼如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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 runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtContent" runat="server" Height="179px" TextMode="MultiLine" Width="350px"></asp:TextBox><br />
<br />
<asp:Button ID="btnMake" runat="server" OnClick="btnMake_Click" Text="生成靜態(tài)頁(yè)" />
</div>
</form>
</body>
</html>
后臺(tái)頁(yè)面主要代碼(Default.aspx.cs):
復(fù)制代碼 代碼如下:
protected void btnMake_Click(object sender, EventArgs e)
{
//替換掉模板中的特征字符
string mbPath = Server.MapPath("template.htm");
Encoding code = Encoding.GetEncoding("UTF-8");
StreamReader sr = null;
StreamWriter sw = null;
string str = null;
//讀取
try
{
sr = new StreamReader(mbPath, code);
str = sr.ReadToEnd();
}
catch (Exception ex)
{
throw ex;
}
finally
{
sr.Close();
}
//根據(jù)時(shí)間自動(dòng)重命名,擴(kuò)展名也可以自行修改
string fileName = DateTime.Now.ToString("yyyyMMddHHmm") + ".htm";
str = str.Replace("$content$", txtContent.Text);//替換content
//生成靜態(tài)文件
try
{
sw = new StreamWriter(Server.MapPath("~/") + fileName, false, code);
sw.Write(str);
sw.Flush();
}
catch (Exception ex)
{
throw ex;
}
finally
{
sw.Close();
Response.Write("<a href=" + fileName + " mce_href=" + fileName + " target=_blank>" + fileName + "</a>已經(jīng)生成!");
}
}
當(dāng)新聞量很大時(shí)這樣做勢(shì)必會(huì)增加服務(wù)器的存儲(chǔ)壓力,暫時(shí)記錄下來(lái)等畢業(yè)設(shè)計(jì)時(shí)再考慮增加動(dòng)態(tài)生成靜態(tài)頁(yè)面,靜態(tài)頁(yè)面分頁(yè)的功能。
您可能感興趣的文章:
- ASP.NET MVC3關(guān)于生成純靜態(tài)后如何不再走路由直接訪問(wèn)靜態(tài)頁(yè)面
- 使用ASP.NET模板生成HTML靜態(tài)頁(yè)面的五種方案
- ASP.NET 生成靜態(tài)頁(yè)面 實(shí)現(xiàn)思路
- Asp.NET 生成靜態(tài)頁(yè)面并分頁(yè)的代碼
- Asp.Net生成靜態(tài)頁(yè)面的實(shí)現(xiàn)方法
- ASP.NET MVC生成靜態(tài)頁(yè)面的方法
- asp.net生成Excel并導(dǎo)出下載五種實(shí)現(xiàn)方法
- asp.net(C#) 生成隨機(jī)驗(yàn)證碼的代碼
- ASP.net(c#)生成條形碼 code39條碼生成方法
- asp.net C#生成和解析二維碼的實(shí)例代碼
- Asp.net生成Excel文件并下載(更新:解決使用迅雷下載頁(yè)面而不是文件的問(wèn)題)
- ASP.NET編程簡(jiǎn)單實(shí)現(xiàn)生成靜態(tài)頁(yè)面的方法【附demo源碼下載】
相關(guān)文章
剖析ASP.NET MVC的DependencyResolver組件
這篇文章主要為大家剖析ASP.NET MVC的DependencyResolver組件,感興趣的小伙伴們可以參考一下2016-04-04ASP.NET?Core配置設(shè)置之Configuration包
這篇文章介紹了ASP.NET?Core配置設(shè)置之Configuration包,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-07-07Asp.net生成Excel文件并下載(更新:解決使用迅雷下載頁(yè)面而不是文件的問(wèn)題)
Asp.net生成Excel文件并下載(更新:解決使用迅雷下載頁(yè)面而不是文件的問(wèn)題)2012-01-01ASP.Net PlaceHolder、Panel等控件未實(shí)現(xiàn)INamingContainer,導(dǎo)致FindContro
這2天在開(kāi)發(fā)中發(fā)現(xiàn),如果在new的Panel中使用FindControl,會(huì)出現(xiàn)找不到控件的情況2009-06-06淺談ASP.NET Core 2.0 布局頁(yè)面(譯)
本篇文章主要介紹了淺談ASP.NET Core 2.0 布局頁(yè)面(譯),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11Discuz!NT數(shù)據(jù)庫(kù)讀寫(xiě)分離方案詳解
Discuz!NT這個(gè)產(chǎn)品在其企業(yè)版中提供了對(duì)‘讀寫(xiě)分離’機(jī)制的支持,使對(duì)CPU及內(nèi)存消耗嚴(yán)重的操作(CUD)被 分離到一臺(tái)或幾臺(tái)性能很高的機(jī)器上,而將頻繁讀取的操作(select)放到幾臺(tái)配置較低的機(jī)器上,然后通過(guò)‘事務(wù) 發(fā)布訂閱機(jī)制’,實(shí)現(xiàn)了在多個(gè)sqlserver數(shù)據(jù)庫(kù)之間快速高效同步數(shù)據(jù),從而達(dá)到了將‘讀寫(xiě)請(qǐng)求’按實(shí)際負(fù)載 情況進(jìn)行均衡分布的效果。2010-06-06