ASP. NET dynamically generates instance code for static pages

  • 2020-06-15 08:01:18
  • OfStack

Recently, I suddenly want to make the news management module in the project into a static page. I find a lot of good articles on the Internet and record 1 here. Now I only realize the generation of static pages, but do not realize paging function. The main idea is to read the data from the database and replace the contents of the static template page.
First, make a template page and temporarily name it 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" >
< html xmlns="http://www.w3.org/1999/xhtml" >
< head >
< title > < /title >
< /head >
< body >
< div >
$content$
< /div >
< /body >
< /html >
Then make a dynamic page, where we click the event with a button to generate a static page.
Front page main code (ES50en. aspx) :

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head runat="server">  
    <title></title>  
</head>  
<body>  
    <form id="form1" runat="server">  
    <div>  
        <asp:TextBox ID="txtContent" runat="server" Height="179px" TextMode="MultiLine" Width="350px"></asp:TextBox><br />  
        <br />  
        <asp:Button ID="btnMake" runat="server" OnClick="btnMake_Click" Text=" Generate static pages " />  
    </div>  
    </form>  
</body>  
</html>  

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

protected void btnMake_Click(object sender, EventArgs e)  
    {  
        // Replace the characteristic 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();  
        }  
        // The name is automatically renamed according to the time, and the extension can be changed by itself      
        string fileName = DateTime.Now.ToString("yyyyMMddHHmm") + ".htm";  
        str = str.Replace("$content$", txtContent.Text);// replace content    
        // Generate static files      
        try  
        {  
            sw = new StreamWriter(Server.MapPath("~/") + fileName, false, code);  
            sw.Write(str);  
            sw.Flush();  
        }  
        catch (Exception ex)  
        {  
            throw ex;  
        }  
        finally  
        {  
            sw.Close();  
            Response.Write("<a href=" + fileName + " mce_href=" + fileName + " target=_blank>" + fileName + "</a> Generated! ");  
        }  
    }  

When the amount of news is very large, this is bound to increase the storage pressure of the server, temporarily recorded when the graduation design, then consider to increase the dynamic generation of static pages, static page paging function.

Related articles: