It is not difficult to generate a single static page. It is difficult to maintain the integrity of the association and links between various static pages;
Especially when pages are frequently updated, modified, or deleted;
For example, Alibaba's pages are all html pages. It is estimated that the address ing function is used.
Let's take a look at this page and analyze his "spot price countdown" function.
The FileSystemObject object used to generate static pages implemented in Asp!
System. IO is involved in. Net.
The following is the program code note: This code is not original! Reference others' code
// Generate an HTML page
The code is as follows: |
Copy code |
Public static bool WriteFile (string strText, string strContent, string strAuthor) { String path = HttpContext. Current. Server. MapPath ("/news /"); Encoding code = Encoding. GetEncoding ("gb2312 "); // Read the template file String temp = HttpContext. Current. Server. MapPath ("/news/text.html "); StreamReader sr = null; StreamWriter sw = null; String str = ""; Try { Sr = new StreamReader (temp, code ); Str = sr. ReadToEnd (); // read the file } Catch (Exception exp) { HttpContext. Current. Response. Write (exp. Message ); HttpContext. Current. Response. End (); Sr. Close (); } String htmlfilename = DateTime. Now. ToString ("yyyyMMddHHmmss") + ". html "; // Replace content // At this time, the template file has been read into the variable named str Str = str. Replace ("ShowArticle", strText); // ShowArticle on the template page Str = str. Replace ("biaoti", strText ); Str = str. Replace ("content", strContent ); Str = str. Replace ("author", strAuthor ); // Write an object Try { Sw = new StreamWriter (path + htmlfilename, false, code ); Sw. Write (str ); Sw. Flush (); } Catch (Exception ex) { HttpContext. Current. Response. Write (ex. Message ); HttpContext. Current. Response. End (); } Finally { Sw. Close (); } Return true; |
This function is stored in the Conn. CS base class.
Reference in the code for adding news: Project name: Hover
The code is as follows:
The code is as follows: |
Copy code |
If (Hover. Conn. WriteFilethis. Title. Text. ToString), this. Content. Text. ToString), this. Author. Text. ToString ))) { Response. Write ("added successfully "); } Else { Response. Write ("An error occurred while generating HTML! "); } |
Template page text.html code
The code is as follows:
The code is as follows: |
Copy code |
<! Doctype html public "-// W3C // dtd html 4.0 Transitional // EN"> <HTML> <HEAD> <Title> ShowArticle </title> <Body> Biaoti <Br> Content <br> Author </Body> </HTML> Biaoti <Br> Content <br> Author </Body> </HTML> |
A prompt is displayed, indicating that an html file with the current time as the file name is displayed! The above only writes the passed parameters to the HTML file. In actual applications, you need to add the database before writing the HTML file.