复制代码 代码如下:
<%@ 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>已经生成!");
}
}
新闻热点
疑难解答
图片精选