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;
using System.IO;
using System.Text;
public partial class WriteFile : System.Web.UI.Page
{
public static bool createHtml(string[] strNewHtml, string[] stroldHtml, string strModeFilePath, string strPath)
{
bool flag = false;
StreamReader sr = null;
StreamWriter sw = null;
string filepath = HttpContext.Current.Server.MapPath(strModeFilePath);
Encoding code = Encoding.GetEncoding("gb2312");
string s = "";
try
{
sr = new StreamReader(filepath, code);
s = sr.ReadToEnd();
}
catch (Exception ex)
{
throw ex;
}
finally
{
sr.Close();
}
try
{
for (int i = 0; i < strNewHtml.Length; i++)
{
s = s.Replace(stroldHtml[i], strNewHtml[i]);
}
sw = new StreamWriter(HttpContext.Current.Server.MapPath(strPath), false, code);
sw.Write(s);
flag = true;
}
catch (Exception ex)
{
flag = false;
throw ex;
}
finally
{
sw.Flush();
sw.Close();
}
return flag;
}
public static bool UpdateHtmlPage(string[] strNewHtml, string[] strStartHtml, string[] strEndHtml, string strHtml)
{
bool Flage = false;
StreamReader ReaderFile = null;
StreamWriter WrirteFile = null;
string FilePath = HttpContext.Current.Server.MapPath(strHtml);
Encoding Code = Encoding.GetEncoding("gb2312");
string strFile = "";
try
{
ReaderFile = new StreamReader(FilePath, Code);
strFile = ReaderFile.ReadToEnd();
}
catch (Exception ex)
{
throw ex;
}
finally
{
ReaderFile.Close();
}
try
{
int intLengTh = strNewHtml.Length;
for (int i = 0; i < intLengTh; i++)
{
int intStart = strFile.IndexOf(strStartHtml[i]) + strStartHtml[i].Length;
int intEnd = strFile.IndexOf(strEndHtml[i]);
string strOldHtml = strFile.Substring(intStart, intEnd - intStart);
strFile = strFile.Replace(strOldHtml, strNewHtml[i]);
}
WrirteFile = new StreamWriter(FilePath, false, Code);
WrirteFile.Write(strFile);
Flage = true;
}
catch (Exception ex)
{
throw ex;
}
finally
{
WrirteFile.Flush();
WrirteFile.Close();
}
return Flage;
}
protected void Button1_Click(object sender, EventArgs e)
{
string NewTitle = "C#将ASPX页面转换为静态页面";
string NewAuthor = "佚名";
string NewContent = "秦迷修订版本";
DateTime PubTime = DateTime.Now;
string[] strNewHtml = new string[] { NewTitle, NewAuthor, NewContent, PubTime.ToString() };
string[] strOldHtml = new string[] { "@Title", "@Author", "@Content", "@PubTime" };
string strDirName = DateTime.Now.ToString("yyyyMM");
string strFileName = DateTime.Now.ToString("yyMMddhhmmss") + ".html";
Directory.CreateDirectory(MapPath(strDirName));
string strFilePath = string.Format(strDirName + "/{0}", strFileName);
try
{
if (WriteFile.createHtml(strNewHtml, strOldHtml, "qinmi.html", strFilePath))
{
Label1.Text = "生成成功!";
}
else
{
Label1.Text = "生成失败!";
}
}
catch
{
Label1.Text = "生成失败!";
}
}
}
------------qinmi.html模板页--------------
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Test</title>
<style type="text/css">
.tb{border: solid 1px #CCCCCC;}
.td{border-bottom:dotted 1px #CCCCCC;}
</style>
</head>
<body>
<table class="tb">
<tr>
<td class="td">
@Title</td>
</tr>
<tr>
<td class="td">
作者:@Author 发布时间:@PubTime
</td>
</tr>
<tr>
<td>
@Content</td>
</tr>
</table>
</body>
</html>
别太看懂,可以在.htm页面里面欠一个aspx页面
参见 http://space.cnblogs.com/question/7061/
---------------------------------------------------------------------------------------------
静态化过程的难度和动态页面“动态”因素成指数关系。如下是我最近应用而写的一个静态化处理抽象类BaseHandlerStaticizer:
using System;
using System.IO;
using System.Text;
using System.Web;
/// <summary>
/// 提供抽象静态化服务.
/// Howard 20090420.
/// </summary>
public abstract class BaseHandlerStaticizer : IHttpHandler {
private const int ScriptTimeout = 60 * 5;// 5 分钟
private HttpContext _context;
protected HttpContext Context {
get { return _context; }
private set {
_context = value;
}
}
/// <summary>
/// 待处理的需要静态化的Url
/// </summary>
protected abstract string[] ProcessUrls {
get;
}
protected abstract bool PreserveForm {
get;
}
protected void Process() {
Context.Server.ScriptTimeout = ScriptTimeout;
StringBuilder builder;
foreach (string url in ProcessUrls) {
builder = new StringBuilder();
using (TextWriter tw = new StringWriter(builder)) {
Context.Server.Execute(url, tw, PreserveForm);
SaveStaticFile(url, builder.ToString());
}
}
}
/// <summary>
/// 保存静态文件
/// </summary>
/// <param name="contents"></param>
/// <param name="url"></param>
protected abstract void SaveStaticFile(string sourceUrl, string contents);
#region IHttpHandler 成员
bool IHttpHandler.IsReusable {
get { return false; }
}
void IHttpHandler.ProcessRequest(HttpContext context) {
try {
Context = context;
Process();
context.Response.ContentType = "text/plain";
context.Response.Write("Ok.");
}
catch (Exception E) {
context.Response.ContentType = "text/plain";
context.Response.Write(E.ToString());
}
}
#endregion
}
主要的静态化方法是 Process 函数, 如下:
Process
protected void Process() {
Context.Server.ScriptTimeout = ScriptTimeout;
StringBuilder builder;
foreach (string url in ProcessUrls) {
builder = new StringBuilder();
using (TextWriter tw = new StringWriter(builder)) {
Context.Server.Execute(url, tw, PreserveForm);
SaveStaticFile(url, builder.ToString());
}
}
}
月经问