Two ways to convert.aspx to.htm
- 2020-06-19 10:07:29
- OfStack
Method 1: Generate from the template and keep it in the html folder
Thinking analysis:
1. Write a custom HTM template where you want to replace it with $value$
Contains up to
2. In ASPX to generate pages, read HTM template with StreamReader and use REPLACE
Replace $value $
3. Output the completed string using StreamWriter
The reference code is as follows:
1) Define the template emplate.htm
2) Write the following code in the event handling of Default. aspx page:
Method 2: Generate a static page hold from the Url address
Thinking analysis:
Translate dynamic pages into static pages directly, so the content generated is not flexible enough
Reference code:
Thinking analysis:
1. Write a custom HTM template where you want to replace it with $value$
Contains up to
2. In ASPX to generate pages, read HTM template with StreamReader and use REPLACE
Replace $value $
3. Output the completed string using StreamWriter
The reference code is as follows:
1) Define the template 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$ Generate static pages 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: $content$ </td>
</tr>
</table>
<a href="#" target="_blank"> All rights reserved </a>
</body>
</html>
2) Write the following code in the event handling of Default. aspx page:
// The source code is to replace the characteristic characters in the template
string mbPath = Server.MapPath("template.htm");
Encoding code = Encoding.GetEncoding("gb2312");
StreamReader sr = null;
StreamWriter sw = null;
string str = null;
// read
try
{
sr = new StreamReader(mbPath, code);
str = sr.ReadToEnd();
}
catch (Exception ex)
{
throw ex;
}
finally
{
sr.Close();
}
// The name is automatically renamed according to the time, and the extension can be changed by itself
string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".htm";
str = str.Replace("$title{1}quot;, txtTitle.Text);// replace Title
str = str.Replace("$content{1}quot;, txtContent.Text);// replace content
// Generate static files
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(" congratulations <a href=htm/" + fileName + " target=_blank>" + fileName + "</a> Generated, saved in htm Under the folder! ");
}
Method 2: Generate a static page hold from the Url address
Thinking analysis:
Translate dynamic pages into static pages directly, so the content generated is not flexible enough
Reference code:
// According to the Url Address generation static page retention
protected void Button2_Click(object sender, EventArgs e)
{
Encoding code = Encoding.GetEncoding("utf-8");
StreamReader sr = null;
StreamWriter sw = null;
string str = null;
// Read remote path
WebRequest temp = WebRequest.Create(txtUrl.Text.Trim());
WebResponse myTemp = temp.GetResponse();
sr = new StreamReader(myTemp.GetResponseStream(), code);
// read
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";
// write
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(" congratulations <a href=htm/" + fileName + " target=_blank>" + fileName + "</a> Generated, saved in htm Under the folder! ");
}
}