Asp.net generate static page code
Asp tutorial. net generate static page code
Private static void createfile (string userid, string filename, string htmlcode)
{
Filename + = ". html ";
String localpath = "e:/www. bKjia. c0m/sun/sundecorativesystem/users/" + userid;
If (! Directory. exists (localpath ))
{
Directory. createdirectory (localpath );
}
Localpath + = "/" + filename;
If (file. exists (localpath ))
{
File. delete (localpath );
}
Filestream fs = new filestream (localpath, filemode. createnew );
Streamwriter sw = new streamwriter (fs );
Sw. autoflush = true;
Htmlcode = htmlcode. replace ("+ ",""");
Sw. write (htmlcode );
Sw. flush ();
Sw. close ();
Fs. close ();
}
// Let's take a look at the complete asp.net tutorial to generate an html code instance.
Environment: microsoft. net framework sdk v1.1
OS: windows server 2003 Chinese Version
Asp.net generates static html pages
The filesystemobject object used to generate static pages implemented in asp!
System. io is involved in. net.
The following is the program code Note: This code is not original! Reference others' code
// Generate an html page
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;
This function is stored in the conn. cs base class.
Reference in the Code for adding news: Project name: hover
If (hover. conn. writefilethis. title. text. tostring), this. content. text. tostring), this. author. text. tostring )))
{
Response. write ("added successfully ");
}
Else
{
Response. write ("An error occurred while generating html! ");
}
-------------------------------------------------------------------------
Template page text.html code
-------------------------------------------------------------------------
<! Doctype html public "-// w3c // dtd html 4.0 transitional // en">
<Html>
<Head>
<Title> showarticle </title>
<Body>
Biaoti
<Br>
Content <br>
Author
</Body>
</Html>