The first is template replacement:
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 is implemented in Asp!
The one involved in such operations in .Net is System.IO
The following is the program code. Note: This code is not original! Please refer to other people’s codes.
//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");
//Read 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 in the template page
str = str.Replace("biaoti",strText);
str = str.Replace("content",strContent);
str = str.Replace("author",strAuthor);
// write 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 and
is referenced in the code for adding news. Note: The project name is Hover
if (Hover.Conn.WriteFilethis.Title.Text.ToString), this.Content.Text.ToString) ,this.Author.Text.ToString)))
{
Response.Write("Added successfully");
}
else
{
Response.Write("Error 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>
After the prompt is added successfully, an html file with the current time as the file name will appear! The above just writes the passed parameters directly into the HTML file.
In actual applications, you need to add the database first and then write the HTML file.
The second type:
WebRequest accesses the aspx page, then gets the Response stream, and gets html
private void button1_Click(object sender, System.EventArgs e)
{
textBox1.Text=this.GetUrlValue(" http://YourUrl ");
}
//Use HttpWebRequest to get the return value of the URL
public string GetUrlValue(string url)
{
System.Net.WebRequest HttpWebRequest=System.Net.WebRequest.Create(url);
System.Net.WebResponse HttpWebResponse =HttpWebRequest.GetResponse();
System.IO.StreamReader sr=new System.IO.StreamReader(HttpWebResponse.GetResponseStream(), System.Text.Encoding.GetEncoding("GB2312"));
return sr.ReadToEnd();
}