Default. aspx this page only has a few textbox controls and two Niu controls
<% @ Page Language = "C #" autoeventwireup = "true" codebehind = "default. aspx. cs" validaterequest = "false" inherits = "tohtml. _ default" %>
<! 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 runat = "server">
<Title> Asp.net static page generation </title>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
Title: <asp: textbox id = "txttitle" runat = "server" width = "352px"> </ASP: textbox> <br/>
Content: <asp: textbox id = "txtcontent" runat = "server" Height = "179px" textmode = "multiline"
Width = "350px"> </ASP: textbox> <br/>
<Br/>
<Asp: button id = "button1" runat = "server" onclick = "button#click" text = "generated by template"/> <br/>
<Br/>
<Br/>
URL address: <asp: textbox id = "txturl" runat = "server" tooltip = "Make sure the URL address exists" width = "359px"> </ASP: textbox>
<Br/>
<Br/>
<Asp: button id = "button2" runat = "server" text = "generate" onclick = "button2_click"/> </div>
</Form>
</Body>
</Html>
Template page to be preparedCodeThe HTM file page is relatively simple. If you are interested, you can make a more complex template page.
! 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 static page 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>
</Body>
</Html>
The default. aspx. CS code for generating static pages in the background is mainly used for file operations.
Sing system;
Using system. Data;
Using system. configuration;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Using system. net;
Using system. text;
Using system. IO;
Namespace tohtml
{
// 51aspx.com: generate a static page demo file. Keep this information for reprinting.
Public partial class _ default: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
}
// Generated based on the template and kept in the HTML folder (some source code is collected on the Network)
Protected void button#click (Object sender, eventargs E)
{
// 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
{< br> sr = new streamreader (mbpath, Code);
STR = sr. readtoend ();
}< br> catch (exception ex)
{< br> throw ex;
}< br> finally
{< br> Sr. close ();
}
// automatically rename the file by time, and the extension can also be modified by yourself
string filename = datetime. now. tostring ("yyyymmddhhmmss") + ". htm ";
STR = Str. replace ("$ title $", txttitle. text); // Replace title
STR = Str. replace ("$ content $", txtcontent. text); // replace content
// generate static files
try
{< br> Sw = new streamwriter (server. mappath ("htm/") + filename, false, Code);
SW. write (STR);
SW. flush ();
}< br> catch (exception ex)
{< br> throw ex;
}< br> finally
{< br> SW. close ();
response. write ("Congratulations " + filename + " generated and saved in the HTM folder! ");
}
}
// 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
{< br> sr = new streamreader (mytemp. getresponsestream (), Code);
STR = sr. readtoend ();
}< br> catch (exception ex)
{< br> throw ex;
}< br> finally
{< br> Sr. close ();
}< br> string filename = datetime. now. tostring ("yyyymmddhhmmss") + ". html ";
// write
try
{
Sw = new streamwriter (server. mappath ("htm/") + filename, false, Code);
SW. write (STR);
SW. flush ();
}< br> catch (exception ex)
{< br> throw ex;
}< br> finally
{< br> SW. close ();
response. write ("Congratulations " + filename + " generated and saved in the HTM folder! ");
}
}
}
}