programming simple method to generate static pages


In this paper, an example is given to show how to generate static page by programming. The details are as follows:

1. Use scenarios

When the data of the page does not need to be changed frequently, the static page mode can be adopted.

2. Benefits of using static pages

(1) Speed up website access

(2) Reduce server burden

(3) Conducive to search engine capture

3. generates static page

There are many ways to generate static pages. Let’s talk about one of the ways I use. reference material

Basic idea:

(1) Create a template template.html file, in which some special string formats are defined to replace the content, such as $htmlformat

(2) Read template and assign value to StringBuilder object

(3) Replace the special string format with what you want

(4) Create a new static page and write the StringBuilder object to the file

4. Method

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.IO;
/// <summary>
///Convert HTML page to generate static page
/// </summary>
public class ConvertHtmlPage
  /// <summary>
  ///Generate HTML file
  /// </summary>
  ///< param name = "templatepath" > template path < / param >
  ///< param name = "templatename" > template name < / param >
  ///< param name = "htmlpath" > path to generate HTML < / param >
  ///< param name = "htmlname" > name of generated HTML < / param >
  ///< param name = "format" > replaced content < / param >
  /// <returns></returns>
  public static bool CreatePage(string templatePath,string templateName, string htmlPath, string htmlName,List<string> format)
      //Read template file
      StringBuilder htmltext = new StringBuilder();
      using (StreamReader sr = new StreamReader(templatePath+templateName))
        string line;
        while ((line = sr.ReadLine()) != null)
      //Replace tag content in HTML
      for (int i = 0; i < format.Count; i++)
        htmltext.Replace("$htmlformat[" + i + "]", format[i]);
      //Generate HTML file
      using (StreamWriter sw = new StreamWriter(htmlPath+htmlName, false, System.Text.Encoding.GetEncoding("GB2312")))
    catch (Exception ex)
      return false;
    return true;

Attached:Demo instance click hereDownload from our website

For more information about, readers who are interested in it can see the special topic of this website:《Summary of file operation skills》、《Summary of the skills of operating JSON with》、《Summary of string operation skills》、《 operation XML skill summary》、《Summary of Ajax skills》And《Summary of cache operation skills》。

I hope that this paper will be helpful to programming.