This article describes the two ways to convert. aspx to. htm, a friend you need can refer to, and hope to help you
Method one: Based on template generation, remain in HTML folder
Thinking Analysis:
1. Write a custom HTM template where you need to replace it with $value$.
Contain it.
2. In the aspx of the generated page, read the HTM template with StreamReader with replace
Replace $value$
3. target= the completed http://www.php.cn/wiki/57.html "_blank" > string with StreamWriter output
The reference code is as follows:
1) define template emplate.htm
The code is as follows:
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
2) write the following code in the Default.aspx page of the button event handling:
The code is as follows:
//source code is to replace 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 by time, extension can also modify string fileName = DateTime.Now.ToString ("YYYYMMDDHHMMSS") + ". htm"; str = str. Replace ("$title {1}quot;, txttitle.text);//substitute title str = str. Replace ("$content {1}quot;, txtcontent.text);//substitute content//generate static file 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:
Translate a good dynamic page directly into a static page, so the generated content is not flexible enough
Reference code:
The code is as follows:
Generate static page 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! "); } }