Two ways to convert. aspx to. htm-Practical tips

Source: Internet
Author: User
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>
&LT;TD height= "align=" center "bgcolor=" #FFFFFF "><span class=" STYLE1 "> $title $ </span></td>
</tr>
<tr>
&LT;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!" ");
}
}

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.