Use ASP. NET and C # To generate static pages

Source: Internet
Author: User
Column code

1.define (template.htm) html template page

<Html>

<Head>

<Title> </title>

<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">

</Head>

<Body>

<Table $ htmlformat [0] height = "100%" border = "0" width = "100%" cellpadding = "10" cellspacing = "0" bgcolor = "# eeeeee" style = "border: 1px solid #000000 ">

<Tr>

<Td width = "100%" valign = "middle" align = "left">

<Span style = "color: $ htmlformat [1]; font-size: $ htmlformat [2]"> $ htmlformat [3] </span>

</Td>

</Tr>

</Table>

</Body>

</Html>

2.asp.net code:

// --------------------- Read the html template page to the stringbuilder object ----

String [] format = new string [4]; // defines an array with the same number of htmlyem tags

StringBuilder htmltext = new StringBuilder ();

Try

{

Using (StreamReader sr = new StreamReader ("path for storing template pages and Page name "))

{

String line;

While (line = sr. ReadLine ())! = Null)

{

Htmltext. Append (line );

}

Sr. Close ();

}

}

Catch

{

Response. Write ("<Script> alert ('file reading error') </Script> ");

}

// --------------------- Assign a value to the tag array ------------

Format [0] = "background = \" bg.jpg \ ""; // background image

Format [1] = "#990099"; // font color

Format [2] = "150px"; // font size

Format [3] = "<marquee> generated template html page </marquee>"; // text description

// ---------- Replace the content marked in htm with the content you want to add

For (int I = 0; I <4; I ++)

{

Htmltext. Replace ("$ htmlformat [" + I + "]", format [I]);

}

// ---------- Generate the htm file ------------------――

Try

{

Using (StreamWriter sw = new StreamWriter ("Storage path and Page name", false, System. Text. Encoding. GetEncoding ("GB2312 ")))

{

Sw. WriteLine (htmltext );

Sw. Flush ();

Sw. Close ();

}

}

Catch

{

Response. Write ("The file cocould not be wirte :");

}

Summary

This method can be used to easily generate html files. The program uses cyclic replacement, so it is very fast for templates that need to replace a large number of elements.
----------------------------------------------------------------
Method 2:
ASP. Net generate static HTML pages
Environment: Microsoft. NET Framework SDK v1.1
OS: Windows Server 2003 Chinese Version
ASP. Net generate 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>






Biaoti
<Br>
Content <br>
Author
</Body>
</HTML>






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.