asp.net generate static page code

Source: Internet
Author: User
Tags flush httpcontext

ASP tutorial. NET generates static page code

private static void CreateFile (string userid, String filename, string htmlcode)
{
filename = = ". html";
String localpath = "e:/www.111cn.net/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 look at a finished ASP.net tutorial to generate an HTML code instance.

Environment: Microsoft. NET Framework SDK v1.1
Os:windows Server 2003 Chinese version
asp.net generate static HTML pages
The FileSystemObject object for generating static pages implemented in ASP!
This type of operation is involved in. NET System.IO
Here is the program code Note: This code is not original! Refer to other people's Code
Generate HTML page

public static bool WriteFile (string strtext,string strcontent,string strauthor)
{
String path = Httpcontext.current.server.mappath ("/news/");
Encoding code = encoding.getencoding ("gb2312");
Reading template files
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 (); Reading files
}
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 point, the template file has been read into a variable named str.
Str =str.replace ("Showarticle", strtext); Showarticle in the template page
str = str.replace ("Biaoti", strtext);
str = str.replace ("content", strcontent);
str = str.replace ("author", Strauthor);
Write a file
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 placed in the Conn.cs base class.
Reference note in code to add News: project name is hover

if (hover.conn.writefilethis.title.text.tostring), this.content.text.tostring), this.author.text.tostring))
{
Response.Write ("Add success");
}
Else
{
Response.Write ("Generate HTML Error!");
}

-------------------------------------------------------------------------
Template page text.html code
-------------------------------------------------------------------------

<!doctype HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >
<title>showarticle</title>

<body>

Biaoti
<br>
Content<br>
Author
</body>

Related Article

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.