.NET 新闻发布系统生成静态页面的实例
<p>经常看到网上的许多CMS系统和新闻发布系统都会在内容发布后生成一个静态页面,这样有助于搜索引擎的收录。因此,思考是否可以在.NET中实现类似的机制。在网上搜索了一些资料后发现,已经有前辈们提供了实现的例子,在此引用一下他们的做法:</p><p>主要做法如下:</p>
<p>1. 创建网站,并创建一个模板页 <code>template.htm</code>。</p>
<p>2. 添加一个Web窗体 <code>Default.aspx</code>。</p>
<p>3. 在网站下新建文件夹 <code>htm</code>,设置该文件夹的属性,确保该文件夹具有可写权限。</p>
<p>详细代码:</p>
<p>1. <code>template.htm</code>:使用 <code>$title$</code> 和 <code>$content$</code> 进行内容替换,类似于 ASP 等生成模板的方式。</p>
<p>
<!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$生成静态页</title>
<style type="text/css">
<!--.STYLE1 {font-size: 16px;font-weight: bold;}-->
</style>
</head>
<body>
<br/>
<br/>
<table width="100%" border="0" bgcolor="#339900">
<tr>
<td height="34" align="center" bgcolor="#FFFFFF"><span> </span></td>
</tr>
<tr>
<td height="42" bgcolor="#FFFFFF"><br/>
<br/>
内容:$content[ DISCUZ_CODE_0 ]nbsp;</td>
</tr>
</table>
</body>
</html>
</p>
<p>2. <code>Default.aspx</code>:生成模板文件名可以自动或是根据定义。</p>
<p>
<%@ 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>Asp.net生成静态页的两个例子</title>
</head>
<body>
<form id="form1" runat="server">
<div>
标题:<asp:TextBox ID="txtTitle" runat="server" Width="352px"></asp:TextBox><br/>
内容:<asp:TextBox ID="txtContent" runat="server" Height="179px" TextMode="MultiLine" Width="350px"></asp:TextBox><br/>
<br/>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="根据模板生成" /><br/>
<br/>
<br/>
Url地址:<asp:TextBox ID="txtUrl" runat="server" ToolTip="请确认Url地址的存在" Width="359px"></asp:TextBox><br/>
<br/>
<asp:Button ID="Button2" runat="server" Text="根据Url地址生成" OnClick="Button2_Click" />
</div>
</form>
</body>
</html>
</p>
<p>3. 后台代码 <code>Default.aspx.cs</code>。</p>
<p>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
using System.Text;
using System.IO;
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
// 根据模板生成,保持在html文件夹中
protected void Button1_Click(object sender, EventArgs e)
{
string mbPath = Server.MapPath("template.htm");
Encoding code = Encoding.GetEncoding("gb2312");
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();
}
// 根据时间自动重命名,扩展名也可以自行修改
string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".htm";
str = str.Replace("$title$", txtTitle.Text); // 替换Title
str = str.Replace("$content$", txtContent.Text); // 替换content
// 生成静态文件
try
{
sw = new StreamWriter(Server.MapPath("htm/") + fileName, false, code);
sw.Write(str);
sw.Flush();
}
catch (Exception ex)
{
throw ex;
}
finally
{
sw.Close();
Response.Write("恭喜<a href=\"htm/" + fileName + "\" target=\"_blank\">" + fileName + "</a>已经生成,保存在htm文件夹下!");
}
}
// 根据Url地址生成静态页保持
protected void Button2_Click(object sender, EventArgs e)
{
Encoding code = Encoding.GetEncoding("utf-8");
StreamReader sr = null;
StreamWriter sw = null;
string str = null;
// 读取远程路径
WebRequest temp = WebRequest.Create(txtUrl.Text.Trim());
WebResponse myTemp = temp.GetResponse();
sr = new StreamReader(myTemp.GetResponseStream(), code);
// 读取
try
{
sr = new StreamReader(myTemp.GetResponseStream(), code);
str = sr.ReadToEnd();
}
catch (Exception ex)
{
throw ex;
}
finally
{
sr.Close();
}
string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".htm";
// 写入
try
{
sw = new StreamWriter(Server.MapPath("htm/") + fileName, false, code);
sw.Write(str);
sw.Flush();
}
catch (Exception ex)
{
throw ex;
}
finally
{
sw.Close();
Response.Write("恭喜<a href=\"htm/" + fileName + "\" target=\"_blank\">" + fileName + "</a>已经生成,保存在htm文件夹下!");
}
}
}
</p>
<p>此代码已经测试通过。</p>
<p>生成静态页面可以根据时间来命名,也可以根据Url地址生成。</p>
页:
[1]