Method 1: generate a file based on the template and keep it in the html folder.
Train of Thought Analysis:
1. Write a custom HTM template with $ value $ to replace it.
Include
2. In the ASPX of the generated page, read the HTM template with StreamReader and use REPLACE
Replace $ value $
3. output the completed string using StreamWriter
The reference code is as follows:
1)template emplate.htm
Copy codeThe Code is as follows:
<! 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 a static page 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"> copyright </a>
</Body>
</Html>
2) on the Default. aspx page, write the following code in the event handling process:
Copy codeThe Code is as follows:
// The Source Code replaces the feature 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 ();
}
// Automatically rename the file according to the time, and the extension can be modified by yourself
String fileName = DateTime. Now. ToString ("yyyyMMddHHmmss") + ". htm ";
Str = str. Replace ("$ title {1} quot;, txtTitle. Text); // Replace the 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> has been generated and saved in the htm folder! ");
}
Method 2: generate static page persistence based on the Url address
Train of Thought Analysis:
The dynamic pages are directly translated into static pages, so the generated content is not flexible enough.
Reference code:
Copy codeThe Code is as follows:
// Generate static page persistence based on the Url
Protected void Button2_Click (object sender, EventArgs e)
{
Encoding code = Encoding. GetEncoding ("UTF-8 ");
StreamReader sr = null;
StreamWriter sw = null;
String str = null;
// Read the 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> has been generated and saved in the htm folder! ");
}
}