Asp.net programming simple method to generate static pages

Time:2021-6-11

In this paper, an example is given to show how to generate static page by asp.net 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. Asp.net 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)
  {
    try
    {
      //Read template file
      StringBuilder htmltext = new StringBuilder();
      using (StreamReader sr = new StreamReader(templatePath+templateName))
      {
        string line;
        while ((line = sr.ReadLine()) != null)
        {
          htmltext.AppendLine(line);
        }
        sr.Close();
      }
      //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")))
      {
        sw.WriteLine(htmltext);
        sw.Flush();
        sw.Close();
      }
    }
    catch (Exception ex)
    {
      return false;
    }
    return true;
  }
}

Attached:Demo instance click hereDownload from our website

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

I hope that this paper will be helpful to asp.net programming.