Method one: Based on template generation, remain in the HTML folder
Thinking Analysis:
1. Write a custom HTM template where you need to replace it with $value$.
Included
2. Generate page aspx, read HTM template with StreamReader, replace
Replace $value$
3. Output the finished string with StreamWriter
The reference code is as follows:
1) define template emplate.htm
Copy Code code as follows:
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<title> $title $ demo|-51aspx.com</title> for generating static pages
<style type= "Text/css" >
<!--
. STYLE1 {
font-size:16px;
Font-weight:bold;
}
-->
</style>
<body>
<br/>
<br/>
<table width= "100%" border= "0" bgcolor= "#339900" >
<tr>
<TD height= "align=" center "bgcolor=" #FFFFFF "><span class=" STYLE1 "> $title $ </span></td>
</tr>
<tr>
<TD height= "bgcolor=" "#FFFFFF" ><br/>
<br/>
Content: $content $ </td>
</tr>
</table>
<a href= "#" target= "_blank" > All rights reserved </a>
</body>
2 Write the following code in the event handling of the Default.aspx page:
Copy Code code as follows:
Source code is to replace the character 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 renamed according to time, extension can also be modified by itself
String fileName = DateTime.Now.ToString ("YYYYMMDDHHMMSS") + ". htm";
str = str. Replace ("$title {1}quot;, txttitle.text);//Replacement title
str = str. Replace ("$content {1}quot;, txtcontent.text);//replacement content
Generating 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, saved in the HTM folder!" ");
}
method Two: Generate static page hold based on URL address
Thinking Analysis:
Directly translate the dynamic pages into static pages, so the generated content is not flexible
Reference code:
Copy Code code as follows:
Generate static page hold based on URL address
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> has been generated, saved in the HTM folder!" ");
}
}