如果你的网站有生成静态页面的必要(个人认为如果能合理缓存定态页面性能应该比使用静态页高^_^),可以参考以下两种方案.
方案1:使用模版页关键字替换
以下是核心代码:
using System;using System.Text;using System.IO;using System.Text.RegularExpressions;using System.Collections.Specialized;
/// <summary>///根据模板页生成静态页/// </summary>public class StaticHtml{ //用来存放要替换的动态数据 static StringDictionary _data;
/// <summary> /// 使用动态数据替换模板内的标签 /// </summary> /// <param name="tmplatehtml"></param> /// <param name="articleinfo"></param> /// <returns></returns> public static string GetHtml(string tmplatehtml, StringDictionary articleinfo) { _data = articleinfo;
// 替换内容 string resltstring; ///使用正则表达式替换捕获模板内所有标签,替换之 resltstring=Regex.Replace(tmplatehtml, "{//$(.*?)//$}", ReplaceTaget,RegexOptions.Multiline);
/* // 如果需要写文件 ,可参照 string htmlfilename = DateTime.Now.ToString("yyyyMMddHHmmss") + ".html"; StreamWriter sw = new StreamWriter(path + htmlfilename , false, Encoding.UTF8); sw.Write(resltstring); sw.Flush(); sw.Close(); */
return resltstring; }
//将单个标签替换成动态数据 static string ReplaceTaget(Match taget) { if (taget.Length > 4) { string tag = taget.Value.Substring(0, taget.Value.Length - 2); tag = tag.Substring(2, tag.Length - 2);
///如果动态数据中包含指定数据,则将标签替换为动态数据,否则则返标签名 return (_data[tag] == null) ? tag : _data[tag].ToString(); } else return taget.Value; }}
方案2:模拟请求动态页面,获取其输出的html,保存为静态页面。
以下是核心代码:
using System;using System.Data;using System.Web;using System.Net;using System.IO;using System.Text;
/// <summary>/// StaticHtml2 的摘要说明/// </summary>public class StaticHtml2{ public static string GetHtml(string Url) { ///定义一个System.Net.WebRequest对象 WebRequest wReq = WebRequest.Create(Url);
// WebRequest请求指定url,返回WebResponse实例 WebResponse wResp = wReq.GetResponse();
// 过去WebResponse里的数据流 Stream respStream = wResp.GetResponseStream();
// 定义一个StreamReader读取Response数据流里的信息 StreamReader reader = new StreamReader(respStream, Encoding.GetEncoding("gb2312"));
string result=reader.ReadToEnd();
/* // 如果需要写文件 ,可参照 string htmlfilename = DateTime.Now.ToString("yyyyMMddHHmmss") + ".html"; StreamWriter sw = new StreamWriter(path + htmlfilename , false, Encoding.UTF8); sw.Write(result); sw.Flush(); sw.Close(); */ return result; }}
测试代码:
default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" Debug="true"%>
<!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> 模版内容<br /> <asp:TextBox ID="temp" runat="server" BackColor="Gainsboro" BorderColor="LightSeaGreen" BorderWidth="1px" Height="100px" TextMode="MultiLine" Width="100%"></asp:TextBox> 处理后的内容<br /> <asp:TextBox ID="replaceed" runat="server" BackColor="Gainsboro" BorderColor="LightSeaGreen" BorderWidth="1px" Height="100px" TextMode="MultiLine" Width="100%"></asp:TextBox></div> </form></body></html>
Default.aspx.cs
using System;using System.Data;using System.Configuration;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;
public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { /* //对指定地址请求方式,将动态页面生成静态 Response.Write(StaticHtml2.GetHtml("http://www.sohu.com")); Response.End(); */
string template = "<html><title>{$title$}</title><body>{$title$}<br/>作者:{$author$}<br/>{$content$}</body></html>"; temp.Text =template;
System.Collections.Specialized.StringDictionary dic = new System.Collections.Specialized.StringDictionary(); dic.Add("title","测试标题"); dic.Add("author", "测试作者"); dic.Add("content", "测试内容");
string result = StaticHtml.GetHtml(template, dic); replaceed.Text = result;
}}
以上代码经本人测试可以正常工作,但实际应用还需根据特定需要改进并完善。祝你编码愉快~