復制代碼 代碼如下:
<%@ 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 runat="server">
<div>
<asp:TextBox runat="server" TextMode="MultiLine"></asp:TextBox><br />
<br />
<asp:Button runat="server" Text="生成靜態頁" />
</div>
</form>
</body>
</html>
復制代碼 代碼如下:
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();
}
//根據時間自動重命名,擴展名也可以自行修改
string fileName = DateTime.Now.ToString("yyyyMMddHHmm") + ".htm";
str = str.Replace("$content$", txtContent.Text);//替換content
//生成靜態文件
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>已經生成!");
}
}
新聞熱點
疑難解答
圖片精選