Asp.net generate static page code

Source: Internet
Author: User

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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.