Core code of the news system that generates static files

Source: Internet
Author: User

I have read a lot of news systems on the Internet that can generate static pages, but there are very few asp.net-based systems. I wrote one for myself and published it. Let's study it together, the code is not optimized, but the function is implemented.

 

Using System;
Using System. Collections;
Using System. ComponentModel;
Using System. Data;
Using System. Data. OleDb;
Using System. Drawing;
Using System. Web;
Using System. Web. SessionState;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Web. UI. HtmlControls;
Using System. Configuration;

Namespace makehtmlfile
{
/// <Summary>
/// Summary of makeallfiles.
/// </Summary>
Public class makeallfiles: System. Web. UI. Page
{
Public string strcon;
Public OleDbConnection conn;
Public string strSQL;

Private void Page_Load (object sender, System. EventArgs e)
{

InitialPages (); // place user code here to initialize the page
}

Public void InitialPages ()
{
Strcon = "provider = Microsoft. jet. OLEDB.4.0; data Source = "+ Server. mapPath (ConfigurationSettings. appSettings ["MDBpath2"]) + ";"; // connection character seek // place user code here to initialize the page
StrSQL = "select id, class1id, class2id from news order by id desc ";
MakeAreaForShow ();
ReadNewsForWriteFileUserDataReader (); // same as DataReader to read data,
// ReadNewsForWriteFileUserDataSet (); // directly attach the data to DataSet for reading,
}
/// <Summary>
/// Used to generate a circular display page area, and load the ASPX page area of the HTML page
/// </Summary>
Public void MakeAreaForShow ()
{
Response. Write ("<span id = showImport> </span> ");
Response. Write ("<IE: Download ID = 'odownload' STYLE = 'behavior: url (# default # download) '/> ");
}

/// <Summary>
/// Read data through DATAREADER
/// </Summary>
Public void ReadNewsForWriteFileUserDataReader ()
{
Int num = 0;
String newsid = null;
String class1id = null;
String class2id = null;
OleDbDataReader dr = null;
OleDbConnection conn = new OleDbConnection (strcon );
Conn. Open ();
OleDbCommand mycommand = new OleDbCommand (strSQL, conn );
Dr = mycommand. ExecuteReader ();
While (dr. Read ())
{
Newsid = dr ["id"]. ToString ();
Class1id = dr ["class1id"]. ToString ();
Class2id = dr ["class2id"]. ToString ();
WriteJScript (newsid, class1id, class2id );
Num ++;
}
Dr. Close ();
Conn. Close ();
Response. Write (num. ToString ());

}

/// <Summary>
/// Read data through DATASET
/// </Summary>
Public void ReadNewsForWriteFileUserDataSet ()
{
DataSet ds = new DataSet ();
Int num = 0;
String newsid = null;
String class1id = null;
String class2id = null;

OleDbConnection conn = new OleDbConnection (strcon );
Conn. Open ();
OleDbDataAdapter da = new OleDbDataAdapter (strSQL, conn );
Da. Fill (ds, "news ");
Conn. Close ();
Num = ds. Tables ["news"]. Rows. Count;
Foreach (DataRow dr in ds. Tables ["news"]. Rows)
{
Newsid = dr ["id"]. ToString ();
Class1id = dr ["class1id"]. ToString ();
Class2id = dr ["class2id"]. ToString ();
WriteJScript (newsid, class1id, class2id );
}
Ds = null;
Response. Write (num. ToString ());

}

Public void WriteJScript (string newsid, string class1id, string class2id)
{
Response. Write ("<script> ");
Response. Write ("function onDownloadDone (downDate )");
Response. Write ("{");
Response. Write ("showImport. innerHTML = downDate ");
Response. Write ("}");
Response. Write ("oDownload. startDownload ('makefile2. aspx? Id = ");
Response. Write (newsid );
Response. Write ("& class1id = ");
Response. Write (class1id );
Response. Write ("& class2id = ");
Response. Write (class2id );
Response. Write ("', onDownloadDone )");
Response. Write ("</script> ");
}

# Code generated by region Web Form Designer
Override protected void OnInit (EventArgs e)
{
//
// CODEGEN: This call is required by the ASP. NET Web form designer.
//
InitializeComponent ();
Base. OnInit (e );
}

/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void InitializeComponent ()
{
This. Load + = new System. EventHandler (this. Page_Load );

}
# Endregion
}
}

 

Using System;
Using System. Collections;
Using System. ComponentModel;
Using System. Data;
Using System. Configuration;
Using System. Data. OleDb;
Using System. Drawing;
Using System. Web;
Using System. IO;
Using System. Web. SessionState;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Web. UI. HtmlControls;

Namespace makehtmlfile
{
/// <Summary>
/// Summary of makefile2.
/// </Summary>
Public class makefile2: System. Web. UI. Page
{
Public string strcon;
Public OleDbConnection conn;

Public string class1id;
Public string class2id;
//***********************************
Public string previd;
Public string prevtitle;
Public string nextid;
Public string nexttitle;
//***********************************
Public string newstitle;
Public string newscontent;
Public string newsdate;
Public string newsip;
Public string newsid;
//************************************

 

Private void Page_Load (object sender, System. EventArgs e)
{
Strcon = "provider = Microsoft. jet. OLEDB.4.0; data Source = "+ Server. mapPath (ConfigurationSettings. appSettings ["MDBpath2"]) + ";"; // connection character seek // place user code here to initialize the page
If (Request. Params ["id"]! = Null & Request. Params ["class1id"]! = Null & Request. Params ["class2id"]! = Null)
{
InitialPages ();

} // Place user code here to initialize the page
}

Public void InitialPages ()
{
Strcon = "provider = Microsoft. jet. OLEDB.4.0; data Source =" + Server. MapPath (ConfigurationSettings. receivettings ["MDBpath2"]) + ";";

If (Request. Params ["id"]! = Null)
{
Newsid = Request. Params ["id"]. ToString ();
}
If (Request. Params ["class1id"]! = Null)
{
Class1id = Request. Params ["class1id"]. ToString ();
}
If (Request. Params ["class2id"]! = Null)
{
Class2id = Request. Params ["class2id"]. ToString ();
}
ReadDataBase (newsid, class2id );
MakeHtmlFile (newsid, class1id, class2id );
}

/// <Summary>
/// Read and write the same category in the previous and next articles
/// </Summary>
/// <Param name = "inputid"> ID of this article </param>
/// <Param name = "class2id"> category ID of the document </param>
Public void ReadPrevAndNext (string inputid, string class2id)
{
Int id = int. Parse (inputid );
String strPrevSQL = "select top 1 id, newstitle, newsdate from news where class2id = '" + class2id + "' and id <" + id + "order by id desc ";
String strNextSQL = "select top 1 id, newstitle, newsdate from news where class2id = '" + class2id + "' and id>" + id + "order by id asc ";
OleDbDataReader datar = null;
OleDbConnection con = new OleDbConnection (strcon );
Con. Open ();
OleDbCommand newcommand = new OleDbCommand (strPrevSQL, con );
Datar = newcommand. ExecuteReader ();
While (datar. Read ())
{
Previd = datar ["id"]. ToString ();
Prevtitle = datar ["newstitle"]. ToString ();
}
Datar. Close ();
Newcommand. CommandText = strNextSQL;
Datar = newcommand. ExecuteReader ();
While (datar. Read ())
{
Nextid = datar ["id"]. ToString ();
Nexttitle = datar ["newstitle"]. ToString ();
}
Con. Close ();

}

/// <Summary>
/// Read the article information from the library and write the HTML file path to the database.
/// </Summary>
/// <Param name = "inputid"> </param>
/// <Param name = "class2id"> </param>
Public void ReadDataBase (string inputid, string class2id)
{
String filename_w = MakeFileName (class1id, class2id, newsid) + ". htm ";

ReadPrevAndNext (inputid, class2id); // read the information of the next and previous articles.

OleDbConnection mycon = new OleDbConnection (strcon); // open the database connection
Mycon. Open ();

Int id = int. Parse (inputid );
String strSQL = "select * from news where id =" + id;
OleDbDataReader dr = null;
OleDbCommand mycommand = new OleDbCommand (strSQL, mycon );
Dr = mycommand. ExecuteReader ();
While (dr. Read ())
{
Newstitle = dr ["newstitle"]. ToString ();
Newscontent = dr ["newscontent"]. ToString ();
Newsdate = dr ["newsdate"]. ToString ();
Newsip = dr ["newsip"]. ToString ();
}
Dr. Close ();
Mycommand. commandText = "update news set url = '" + filename_w + "'where id =" + int. parse (inputid); // writes the generated file path to the database for easy use on the generated category page.
Mycommand. ExecuteNonQuery ();
Mycon. Close ();

}

/// <Summary>
/// Generate the target directory and file, mainly used to generate directories of different categories
/// </Summary>
/// <Param name = "inputstr"> </param>
/// <Returns> </returns>
Public string MakeCatalogName (string class1, string class2) // generate the target directory file
{
String namestr = "Article ";
String rootstr = Server. MapPath ("."). ToString ();
String class1str = rootstr + "\" + namestr + "_" + class1 + "\\";
String class2str = rootstr + "\" + namestr + "_" + class1 + "\" + namestr + "_" + class2 + "\\";
If (! Directory. Exists (class1str ))
{
Directory. CreateDirectory (class1str );
}
If (! Directory. Exists (class2str ))
{
Directory. CreateDirectory (class2str );
}
// Create the target folder
Return class2str;
}

/// <Summary>
/// Generate a file name based on the document category and ID
/// </Summary>
/// <Param name = "class1id"> </param>
/// <Param name = "class2id"> </param>
/// <Param name = "nid"> </param>
/// <Returns> returned file name </returns>
Public string MakeFileName (string class1, string class2, string id) // generate the file name to generate the previous
{
String myclass2id = class2;
String myclass1id = class1;
String s = DateTime. Now. Year. ToString ()
+ DateTime. Now. Month. ToString ()
+ DateTime. Now. Day. ToString ()
+ "_"
+ Myclass1id
+ "_"
+ Myclass2id // parent class ID
+ "_"
+ Id; // news ID
Return s;
}

/// <Summary>
/// Generate an HTML file
/// </Summary>
/// <Param name = "nid"> ID of the document </param>
Public void MakeHtmlFile (string nid, string cla1id, string cla2id) // MakeHtmlFile (string nid, string cla1id, string cla2id, string filetemp) is used to differentiate different classes
{
String file_path = Server. MapPath ("template/news_mb.htm ");
String desfilename = MakeFileName (cla1id, cla2id, nid) + ". htm ";
String desfile = MakeCatalogName (cla1id, cla2id) + MakeFileName (cla1id, cla2id, nid) + ". htm ";
String prevurl = MakeFileName (cla1id, cla2id, previd) + ". htm"; // generate the file connection name in the previous article based on the category and ID
String nexturl = MakeFileName (cla1id, cla2id, nextid) + ". htm"; // next
System. Text. Encoding code = System. Text. Encoding. GetEncoding ("gb2312 ");

StreamReader srd = null; // read
StreamWriter swr = null; // write

String strFile = null; // string

Try
{
Srd = new StreamReader (file_path, code );
StrFile = srd. ReadToEnd (); // read the file
}
Catch (Exception exp)
{
HttpContext. Current. Response. Write (exp. Message );
HttpContext. Current. Response. End ();
Srd. Close ();
}

StrFile = strFile. Replace ("$ title $", newstitle );
StrFile = strFile. Replace ("$ content $", newscontent );
StrFile = strFile. Replace ("$ date $", newsdate );
StrFile = strFile. Replace ("$ ip $", newsip );
StrFile = strFile. Replace ("$ prev $", prevtitle );
StrFile = strFile. Replace ("$ next $", nexttitle );
StrFile = strFile. Replace ("$ prevurl $", prevurl );
StrFile = strFile. Replace ("$ nexturl $", nexturl );
 
Try
{
Swr = new StreamWriter (desfile, false, code );
Swr. Write (strFile );
Swr. Flush ();
}
Catch (Exception ex)
{
HttpContext. Current. Response. Write (ex. Message );
HttpContext. Current. Response. End ();
}
Finally
{
Swr. Close ();
}
If (srd! = Null)
{
Srd. Close ();
}
Response. Write (desfilename );

}

Public void Open ()
{
If (conn = null)
{
Conn = new OleDbConnection (strcon );
Conn. Open ();
}
}
Public void Close ()
{
If (conn! = Null)
{
Conn. Close ();
}
}

# Code generated by region Web Form Designer
Override protected void OnInit (EventArgs e)
{
//
// CODEGEN: This call is required by the ASP. NET Web form designer.
//
InitializeComponent ();
Base. OnInit (e );
}

/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void InitializeComponent ()
{
This. Load + = new System. EventHandler (this. Page_Load );
}
# Endregion
}
}

 

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.