Two methods of converting. aspx to. html

Source: Internet
Author: User
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!             "); }         }
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.