Implementation Code for generating static Web pages in Asp.net, and static Web pages in asp.net

Source: Internet
Author: User

Implementation Code for generating static Web pages in Asp.net, and static Web pages in asp.net

Currently, all programs need to convert dynamic pages into static pages. Today, we will teach you how to generate static pages in ASP. NET.

Using System;
Using System. Data;
Using System. Configuration;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Web. UI. WebControls. WebParts;
Using System. Web. UI. HtmlControls;
Using System. IO;
Using System. Text;
///
/// Summary of WriteFile
///
Public class WriteFile
{
Public WriteFile ()
{
}
Public static bool createHtml (string [] strnewsHtml, string [] stroldHtml, string strModeFilePath, string strPath)
{
Bool flag = false;
StreamReader sr = null;
StreamWriter sw = null;
String filepath = HttpContext. Current. Server. MapPath (strModeFilePath );
Encoding code = Encoding. GetEncoding ("gb2312 ");
String s = string. Empty;
Try
{
Sr = new StreamReader (filepath, code );
S = sr. ReadToEnd ();
}
Catch (Exception ex)
{
Throw ex;
}
Finally
{
Sr. Close ();
}
Try
{
For (int I = 0; I <strnewsHtml. Length; I ++)
{
S = s. Replace (stroldHtml [I], strnewsHtml [I]);
}
Sw = new StreamWriter (HttpContext. Current. Server. MapPath (strPath), false, code );
Sw. Write (s );
Flag = true;
}
Catch (Exception ex)
{
Flag = false;
Throw ex;
}
Finally
{
Sw. Flush ();
Sw. Close ();
}
Return flag;
}
Public static bool UpdateHtmlPage (string [] strNewsHtml, string [] strStartHtml, string [] strEndHtml, string strHtml)
{
Bool Flage = false;
StreamReader ReaderFile = null;
StreamWriter WrirteFile = null;
String FilePath = HttpContext. Current. Server. MapPath (strHtml );
Encoding Code = Encoding. GetEncoding ("gb2312 ");
String strFile = string. Empty;
Try
{
ReaderFile = new StreamReader (FilePath, Code );
StrFile = ReaderFile. ReadToEnd ();
}
Catch (Exception ex)
{
Throw ex;
}
Finally
{
ReaderFile. Close ();
}
Try
{
Int intLengTh = strNewsHtml. Length;
For (int I = 0; I <intLengTh; I ++)
{
Int intStart = strFile. IndexOf (strStartHtml [I]) + strStartHtml [I]. Length;
Int intEnd = strFile. IndexOf (strEndHtml [I]);
String strOldHtml = strFile. Substring (intStart, intEnd-intStart );
StrFile = strFile. Replace (strOldHtml, strNewsHtml [I]);
}
WrirteFile = new StreamWriter (FilePath, false, Code );
WrirteFile. Write (strFile );
Flage = true;
}
Catch (Exception ex)
{
Throw ex;
}
Finally
{
WrirteFile. Flush ();
WrirteFile. Close ();
}
Return Flage;
}
}
Call public classes:
----------------------------------------------------------------------------
Protected void Button2_Click (object sender, EventArgs e)
{
String NewsTitle = this. TextBox1.Text;
String NewsKindName = this. DropDownList1.SelectedItem. Text;
String NewsBody = this. WebEditor1.Text;
DateTime PubTime = DateTime. Now;
String UserName = Session ["UserName"]. ToString ();
Response. Write (NewsKindName );
String [] strNewsHtml = new string [] {NewsTitle, NewsKindName, NewsBody, PubTime. ToString (), UserName };
String [] strOldHtml = new string [] {"@ Title", "@ NewsKInd", "@ NewsBody", "@ PubTime", "@ UserName "};
String strFileName = DateTime. Now. ToString ("ddhhmmss") + ". html ";
String strFilePath = string. Format ("NewsHtml/{0}", strFileName );
Try
{
If (WriteFile. createHtml (strNewsHtml, strOldHtml, "mode.htm", strFilePath ))
{
This. Label1.Text = "generated successfully! ";
}
Else
{
This. Label1.Text = "generation failed! ";
}
}
Catch
{
This. Label1.Text = "generation failed! ";
}
}
Protected void Button3_Click (object sender, EventArgs e)
{
String [] strNewsHtml = new string [] {"this is a tour! "};
String [] strStartHtml = new string [] {""};
String [] strEndHtml = new string [] {""};
If (WriteFile. UpdateHtmlPage (strNewsHtml, strStartHtml, strEndHtml, "NewsHtml/02011139.html "))
{
This. Label1.Text = "the homepage is generated successfully! ";
}
Else
{
This. Label1.Text = "An error occurred while generating the homepage! ";
}
}

Create the NewsHtml folder and place the generated html file in it.

-----------------------------------------------------------

Add a template file

<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> No title page </title>
</Head>
<Body>
<Table border = "1" cellpadding = "0" cellspacing = "0"> Reprinted from: http://www.aspnetjia.com

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.