Asp. NET generates instance code for static pages dynamically

Source: Internet
Author: User
Tags xmlns
Generate static page has many benefits, can alleviate the server pressure, the convenient search website search and so on, the following introduction generates the static page The instance code, has the need friend may refer to

Recently suddenly want to put the project in the news management module to make static pages, found many good articles on the Internet, here to record, now just to achieve static page generation and did not implement paging function. The main principle is to read the database data and then replace the contents of the static template page.
First make a template page, temporarily named template.htm, the sample code is as follows:
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<title></title>
<body>
<div>
$content $
</div>
</body>
Then make a dynamic page where we use a button click event to generate a static page.
Front page main code (Default.aspx):

Copy Code code as follows:


&lt;%@ Page language= "C #" autoeventwireup= "true" codefile= "Default.aspx.cs" inherits= "_default"%&gt;


&lt;! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "&gt;


&lt;html xmlns= "http://www.w3.org/1999/xhtml" &gt;


&lt;head runat= "Server" &gt;


&lt;title&gt;&lt;/title&gt;


&lt;/head&gt;


&lt;body&gt;


&lt;form id= "Form1" runat= "Server" &gt;


&lt;div&gt;


&lt;asp:textbox id= "txtcontent" runat= "Server" height= "179px" textmode= "MultiLine" width= "350px" &gt;&lt;/asp : textbox&gt;&lt;br/&gt;


&lt;br/&gt;


&lt;asp:button id= "Btnmake" runat= "Server" onclick= "Btnmake_click" text= "Generate static page"/&gt;


&lt;/div&gt;


&lt;/form&gt;


&lt;/body&gt;


&lt;/html&gt;


Background page main code (Default.aspx.cs):

Copy Code code as follows:


protected void Btnmake_click (object sender, EventArgs e)


    { 


//Replace the feature characters in the template


string mbpath = Server.MapPath ("template.htm");


Encoding code = encoding.getencoding ("UTF-8");


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 ();


        } 


//automatic renaming based on time, extension can also be modified


string fileName = DateTime.Now.ToString ("yyyymmddhhmm") + ". htm";


str = str. Replace ("$content $", txtcontent.text);//replacement content


//Generate static file


Try


        { 


SW = new StreamWriter (Server.MapPath ("~/") + FileName, false, code);


Sw.  Write (str);


SW.  Flush ();


        } 


catch (Exception ex)


        { 


throw ex;


        } 


finally


        { 


SW.  Close ();


Response.Write ("&lt;a href=" + filename + "mce_href=" + filename + "target=_blank&gt;" + filename + "&lt;/a&gt; has been generated!"  ");


        } 


    } 


When the news volume is very large, this will increase the storage pressure on the server, temporarily record the graduation design and then consider adding dynamic static page, static page paging function.

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.