asp.net| Static | page
When it comes to the news system, it will certainly talk about the static page generated, because the static page is not only read fast, but also safe;
Static page generation Whether it is small to the current corporate web site to NetEase, QQ and other portals are used;
So how do we generate static pages?
How do I generate a static page ...
There are some things to be aware of when generating static pages:
Static page naming
Unified Storage Directory
Static page templates
Page generation
Generally speaking, on the basis of the original news system, we can regenerate the content according to the request of Get this page (for example: http;//www.test.com/news.aspx?id=1,get This page code to write directly to a text file and in HTML name);
Here I used is the template generation, first with DW to do a Web page template, the title, content, etc. will be dynamically realized in the content of the first to replace, such as $title$, etc. in the generation of replacement into the content of the news;
Name: At the time of generation, it is usually a string that is converted into a press release, which is not repeated.
In addition, I also kept these static files in different directories by year months for easy management,
Call Method Writenews () Given the ID of the parameter based on the IDs of one news, it reads the content from the database based on this ID, and then generates a new static page based on the static template page html/test.html the table of contents in the corresponding year month
Okay, here's the code:
Using System;
using System.IO;
using System.Web;
using System.Text;
Namespace Powerleader.components
... {
/**////<summary>
///writetohtml's summary description.
///</summary>
public class writetohtml
... {
public writetohtml ()
... {
//
//TODO: Add constructor logic here
//
}
public static void writenews (int id)
... {
News = new News ();
News.newsdetails newsdetails = new PowerLeader.Components.News.NewsDetails ();
Newsdetails = news. Getnews (ID);
BOOL Flag;
Flag = WriteFile (newsdetails);
}
public static bool WriteFile (News.newsdetails newsdetails)
... {
Directory.CreateDirectory (HttpContext.Current.Server.MapPath ("/powerleader/html/" +newsdetails.addtime.tostring ("yyyy") + "/" +newsdetails.addtime.tostring ("MM"));
String path = HttpContext.Current.Server.MapPath (".. /html/"+newsdetails.addtime.tostring (" yyyy ") +"/"+newsdetails.addtime.tostring (" MM ") +"/");
Encoding code = encoding.getencoding ("gb2312");
Reading template files
String temp = HttpContext.Current.Server.MapPath (".. /html/text.html ");
StreamReader sr = null;
StreamWriter sw = null;
String stringtempcode = "";
Try
... {
sr = new StreamReader (temp, code);
Stringtempcode = Sr. ReadToEnd (); Reading files
}
catch (Exception exp)
... {
HttpContext.Current.Response.Write (exp. message);
HttpContext.Current.Response.End ();
Sr. Close ();
}
String htmlfilename = newsDetails.addtime.ToString ("YYYYMMDDHHMMSS") + ". html";
Replace content
At this point, the template file has been read into a variable named str.
Stringtempcode = Stringtempcode.replace ("$PageTitle $", "anti-Japanese War online official website ...");
Stringtempcode = Stringtempcode.replace ("$Type $", newsDetails.type.ToString (). Trim ());
Stringtempcode = Stringtempcode.replace ("$Author $", newsDetails.author.ToString (). Trim ());
Stringtempcode = Stringtempcode.replace ("$From $", NewsDetails.from.Trim ());
Stringtempcode = Stringtempcode.replace ("$Time $", newsDetails.addtime.ToString (). Trim ());
Stringtempcode = Stringtempcode.replace ("$Title $", NewsDetails.title.Trim ());
Stringtempcode = Stringtempcode.replace ("$Content $", newsdetails.content);
Write a file
Try
... {
SW = new StreamWriter (path + Htmlfilename, false, code);
Sw. Write (Stringtempcode);
Sw. Flush ();
}
catch (Exception ex)
... {
HttpContext.Current.Response.Write (ex. message);
HttpContext.Current.Response.End ();
}
Finally
... {
Sw. Close ();
}
return true;
}
}
}