将.aspx转换为.htm的两种方法
程序员文章站
2024-03-01 17:18:16
方法一:根据模板生成,保持在html文件夹中 思路分析: 1.写一个自定义的htm模板 其中需要替换的地方用$value$这样 包含起来 ...
方法一:根据模板生成,保持在html文件夹中
思路分析:
1.写一个自定义的htm模板 其中需要替换的地方用$value$这样
包含起来
2.生成页面的aspx中,用streamreader读取htm模板,用replace
替换$value$
3.把完成的字符串用streamwriter输出
参考代码如下:
1)定义模板emplate.htm
<!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$ 生成静态页的demo|-51aspx.com</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 class="style1">$title$ </span></td>
</tr>
<tr>
<td height="42" bgcolor="#ffffff"><br />
<br />
内容:$content$ </td>
</tr>
</table>
<a href="#" target="_blank">版权所有</a>
</body>
</html>
2)在default.aspx页面的按扭的事件处理中写如下代码:
//源码是替换掉模板中的特征字符
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{1}quot;, txttitle.text);//替换title
str = str.replace("$content{1}quot;, 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地址生成静态页保持
思路分析:
直接将做好的动态页面翻译成静态页面,所以生成的内容不够灵活
参考代码:
//根据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文件夹下!");
}
}
思路分析:
1.写一个自定义的htm模板 其中需要替换的地方用$value$这样
包含起来
2.生成页面的aspx中,用streamreader读取htm模板,用replace
替换$value$
3.把完成的字符串用streamwriter输出
参考代码如下:
1)定义模板emplate.htm
复制代码 代码如下:
<!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$ 生成静态页的demo|-51aspx.com</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 class="style1">$title$ </span></td>
</tr>
<tr>
<td height="42" bgcolor="#ffffff"><br />
<br />
内容:$content$ </td>
</tr>
</table>
<a href="#" target="_blank">版权所有</a>
</body>
</html>
2)在default.aspx页面的按扭的事件处理中写如下代码:
复制代码 代码如下:
//源码是替换掉模板中的特征字符
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{1}quot;, txttitle.text);//替换title
str = str.replace("$content{1}quot;, 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地址生成静态页保持
思路分析:
直接将做好的动态页面翻译成静态页面,所以生成的内容不够灵活
参考代码:
复制代码 代码如下:
//根据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文件夹下!");
}
}
上一篇: MySQL循环语句之while循环测试