During website creation, some pages are often generated as static pages to improve the webpage access speed. How can ASP. NET pages generate static pages? Let's explore:
1. To generate a static webpage, you must first have a webpage template. The simplest template is provided below. For more complex templates, you can adapt them to this template:
<Html xmlns = "http://www.w3.org/1999/xhtml">
Here: <$ strText $>, <$ strContent $>, and <$ strAuthor $> are the characters we need to replace. We use special symbols to represent
2. With the template, we will use it in the program:
<1. Create a path to save the static webpage:
String path = HttpContext. Current. Server. MapPath ("news /");
<2. Set the encoding method:
Encoding code = Encoding. GetEncoding ("gb2312 ");
<3. Read the template file.
String temp = HttpContext. Current. Server. MapPath ("Template/text.html ");
<4. Read all the content in the template:
String str = "";
Try
{
Sr = new StreamReader (temp, code); // stream and code to be read
Str = sr. ReadToEnd (); // read the stream from the current position of the stream to the end
}
Catch (Exception exp)
{
HttpContext. Current. Response. Write (exp. Message );
HttpContext. Current. Response. End ();
Sr. Close ();
}
<5. Replacement content:
Str = str. Replace ("<$ strText $>", strText );
Str = str. Replace ("<$ strContent $>", strContent );
Str = str. Replace ("<$ strAuthor $>", strAuthor );
<6. Write the replaced file to the file:
Try
{
Sw = new StreamWriter (path + htmlfilename, false, code );
Sw. Write (str); // Write a file
Sw. Flush (); // clear all Buffers
}
Catch (Exception exp)
{
HttpContext. Current. Response. Write (exp. Message );
HttpContext. Current. Response. End ();
}
Finally
{
Sw. Close (); // Close the write operation
}
The following is a complete example (I have an error inserting a code block. I hope you will forgive me ):
Public static bool WriteFile (string strText, string strContent, string strAuthor)
{
// Create a path to save the static webpage
String path = HttpContext. Current. Server. MapPath ("news /");
// Set the encoding method
Encoding code = Encoding. GetEncoding ("gb2312 ");
// Read the Template File
String temp = HttpContext. Current. Server. MapPath ("Template/text.html ");
// Define the read/write object
StreamReader sr = null;
StreamWriter sw = null;
// Read all the content in the template and put it in str
String str = "";
Try
{
Sr = new StreamReader (temp, code); // stream and code to be read
Str = sr. ReadToEnd (); // read the stream from the current position of the stream to the end
}
Catch (Exception exp)
{
HttpContext. Current. Response. Write (exp. Message );
HttpContext. Current. Response. End ();
Sr. Close ();
}
// Name the production static webpage, with the time name
String htmlfilename = DateTime. Now. ToString ("yyyyMMddHHmmss") + ". html ";
// Replace content
// The template variable has been read to the str variable.
Str = str. Replace ("<$ strText $>", strText );
Str = str. Replace ("<$ strContent $>", strContent );
Str = str. Replace ("<$ strAuthor $>", strAuthor );
// Write an object
Try
{
// False indicates that if the file exists, the file will be overwritten.
// True indicates that if the file exists, content will be appended to the file.
// Otherwise, create the file --- (complete file name (path + file name), whether to append to the file, encoding method)
Sw = new StreamWriter (path + htmlfilename, false, code );
Sw. Write (str); // Write a file
Sw. Flush (); // clear all Buffers
}
Catch (Exception exp)
{
HttpContext. Current. Response. Write (exp. Message );
HttpContext. Current. Response. End ();
}
Finally
{
Sw. Close (); // Close the write operation
}
Return true;
}
For more information, see.