SoFunction
Updated on 2025-03-06

An example explanation of the C# website generating static pages

In some websites that need to update page data frequently, generally, those who do not have a large number of visits are directly posted with backend code, and each visit is in database interaction. However, once the number of visits increases, the cost of these servers will be taken into account. For example, after the background editing, the article content is stored in the database. If 1,000 people visit it, if the database is still fetched every time, then these 1,000 io visits will appear to be relatively large. A good method is to make a static page after the article is determined. This method is done by the program, which is to recursively traverse the entire website, access the website content, and then generate static text pages of these pages, and publish these pages. In this way, for the browser, he still sees the same address and the same article, but this one is static. This improves the efficiency of the website and saves resources;

Here is a copy of C# traversing the website content and then generate the content page code:

private ArrayList htmlCreatedList = new ArrayList();
    /// <summary>
    /// Recursively implement the static function of page    /// </summary>
    /// <param name="urlString">The link address of the page to be accessed</param>    public void SaveHtmlCode(string urlString)
    {
      if ((urlString))
      {
        return;
      }
      string htmlCode = GetHtmlCodeFromUrl(urlString);
      string htmlPath = ();
      string direcHtmlPath = (htmlPath);
      if (!(direcHtmlPath))
      {
        (direcHtmlPath);
      }
      (htmlPath, htmlCode);
      (urlString);
      var urlList = GetUrlLinkFromHtmlCode(htmlCode);
      string urlTemp = ;
      foreach (string url in urlList)
      {
        urlTemp = url;
        urlTemp = (urlTemp, "href\\s*=\\s*", "");
        urlTemp = ("\"", "");
        urlTemp = ("\\", "/");
        urlTemp =  + urlTemp;
        SaveHtmlCode(urlTemp);
      }
    }
    /// &lt;summary&gt;
    /// html code linked through HttpWebRequest page    /// &lt;/summary&gt;
    /// <param name="urlString">Page link address</param>    /// <returns>Hhtml code corresponding to the page link</returns>    private string GetHtmlCodeFromUrl(string urlString)
    {
      HttpWebRequest hwRequest = (HttpWebRequest)(urlString);
       = "User-Agent:Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705";
       = "*/*";
       = true;
      ("Accept-Language", "zh-cn,en-us;q=0.5");
      HttpWebResponse hwResponse = (HttpWebResponse)();
      Stream streamResponse = ();
      StreamReader readerOfStream = new StreamReader(streamResponse, ("utf-8"));
      string strHtml = ();
      ();
      ();
      ();
      return strHtml;
    }
    ///&lt;summary&gt;
    ///The regular expression matches the hyperlink in the html code    ///&lt;/summary&gt;
    ///<param name="htmlCode">To find the html code for the hyperlink</param>    ///&lt;returns&gt;&lt;/returns&gt;
    private IEnumerable&lt;string&gt; GetUrlLinkFromHtmlCode(string htmlCode)
    {
      string strRegex = "href\\s*=\\s*(?:[\"'](?&lt;1&gt;[^\"'.#:]*)[\"'])";
      Regex r = new Regex(strRegex, );
      MatchCollection ms = (htmlCode);
      IEnumerable&lt;string&gt; listUrl = from Match cc in ms select ().Replace("&amp;", "&amp;");
      return ();
    }
  }

Extended a method for string.

public static string ToPhysicalPath(this string urlString)
    {
       uri = new (urlString);
      string htmlPath = ("{0}\\Html\\{1}\\", , );
      string[] querys = (new char[] { '?', '&', '=' }, );
      htmlPath += (, querys);
      htmlPath += (0) ? "" : ".html";
      htmlPath = ("/", "\\");
      htmlPath = ("\\\\", "\\");
      return htmlPath;
    }

Summarize

The above is the entire content of this article. I hope that the content of this article has certain reference value for your study or work. Thank you for your support. If you want to know more about it, please see the following links