The most common method is to read a template from a file, replace the parameters in the template, and output the file. This method is generated much faster than the first method, and the template content can be edited with tools.
Main code:
Using system;
Using system. collections;
Using system. componentmodel;
Using system. data;
Using system. drawing;
Using system. web;
Using system. web. sessionstate;
Using system. web. ui;
Using system. web. ui. webcontrols;
Using system.web.ui.html controls;
Using system. io;
Using system. text;
Namespace xinxi
{
/// <Summary>
/// Summary of createpage.
/// </Summary>
// Www.111cn.net
// This is a small program that generates static web pages.
Public class create
{
Public void createpage ()
{
}
Public static bool writefile (string strtext, string strcontent, string strauthor)
{
String path = httpcontext. current. server. mappath ("/test/"); // file output Directory
Encoding code = encoding. getencoding ("gb2312 ");
// Read the template file
String temp = httpcontext. current. server. mappath ("/template/test.html"); // template file
Streamreader sr = null;
Streamwriter sw = null;
String str = "";
Try
{
Sr = new streamreader (temp, code );
Str = sr. readtoend (); // read the file
}
Catch (exception exp)
{
Httpcontext. current. response. write (exp. message );
Httpcontext. current. response. end ();
Sr. close ();
}
String htmlfilename = datetime. now. tostring ("yyyymmddhhmmss") + ". html"; // static file name
// Replace content
// At this time, the template file has been read into the variable named str
Str = str. replace ("showarticle", strtext); // showarticle on the template page
Str = str. replace ("biaoti", strtext );
Str = str. replace ("content", strcontent );
Str = str. replace ("author", strauthor );
// Write an object
Try
{
Sw = new streamwriter (path + htmlfilename, false, code );
Sw. write (str );
Sw. flush ();
}
Catch (exception ex)
{
Httpcontext. current. response. write (ex. message );
Httpcontext. current. response. end ();
}
Finally
{
Sw. close ();
}
Return true;
}
}
}
// The principle is to use the class in system. io to read and write the template file, replace the tag in the template with replace, and write the static html
See the instance below
Template
<! Doctype html public "-// w3c // dtd html 4.0 transitional // en">
<Html>
<Head>
<Title> showarticle </title>
<Body>
Biaoti
<Br>
Content <br>
Author
</Body>
</Html>
Biaoti
<Br>
Content <br>
Author
</Body>
</Html>
C code
Public static bool writefile (string strtext, string strcontent, string strauthor)
{
String path = httpcontext. current. server. mappath ("/news /");
Encoding code = encoding. getencoding ("gb2312 ");
// Read the template file
String temp = httpcontext. current. server. mappath ("/news/text.html ");
Streamreader sr = null;
Streamwriter sw = null;
String str = "";
Try
{
Sr = new streamreader (temp, code );
Str = sr. readtoend (); // read the file
}
Catch (exception exp)
{
Httpcontext. current. response. write (exp. message );
Httpcontext. current. response. end ();
Sr. close ();
}
String htmlfilename = datetime. now. tostring ("yyyymmddhhmmss") + ". html ";
// Replace content
// At this time, the template file has been read into the variable named str
Str = str. replace ("showarticle", strtext); // showarticle on the template page
Str = str. replace ("biaoti", strtext );
Str = str. replace ("content", strcontent );
Str = str. replace ("author", strauthor );
// Write an object
Try
{
Sw = new streamwriter (path + htmlfilename, false, code );
Sw. write (str );
Sw. flush ();
}
Catch (exception ex)
{
Httpcontext. current. response. write (ex. message );
Httpcontext. current. response. end ();
}
Finally
{
Sw. close ();
}
Return true;
}