主要思路还是使用StreamReader获取远程url的数据流,然后写.html文件来静态输出。
主要类common.cs封装如下:


1

2



3

4



5

6


7

8

9

10

11

12

13

14



15

16

17

18

19

20

21

22

23

24

25

26

27



28

29

30

31

32

33

34



35

36

37

38

39

40



41

42

43

44

45

46

47



48

49

50

51



52

53

54

55

56

57

58

59

60

61



62

63

64

65



66

67

68

69

70

71

72

73

74

以下为页面文件news_list.aspx(前一段时间写的,那时还没有用到存储过程分页)


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

其中带$...$标签的为页码标记。
以下是生成代码,生成内容包括这里的news_list.aspx和news_info.aspx文件,也就是所有关于news的文件。


//全部生成html
protected void MakeHtml(object sender, EventArgs e)
{
btnMakeHtml.Enabled = false;
btnMakeHtml.Text = "生成可能需要一段时间,请耐心等待……";
common cmn = new common();
string url = "http://" + Request.ServerVariables["Http_Host"] + Request.ServerVariables["URL"];
url = url.Substring(0, url.LastIndexOf("/"));
url = url.Substring(0, url.LastIndexOf("/"));
url = url + "/template/";
string fp = Server.MapPath("manage_news.aspx");
fp = fp.Substring(0, fp.LastIndexOf("\\"));
fp = fp.Substring(0, fp.LastIndexOf("\\"));
fp = fp + "\\html\\news\\";
//生成news_list.aspx
if (!Directory.Exists(fp))
{
Directory.CreateDirectory(fp);
}
else
{
string urlList;//获取数据网址
string fpHtml;//生成文件名(含路径)
// int iPageSize = Hope_T_NewManager.GetHope_T_NewsNum();
IList<Hope_T_NewsClass> listNewsClass = Hope_T_NewsClassManager.GetAllHope_T_NewsClassesIndex();
foreach (Hope_T_NewsClass list in listNewsClass)
{
int iNewsNumByClass = Hope_T_NewManager.GetHope_T_NewsNumByNews_ClassId(list.News_ClassId);
if (iNewsNumByClass > 30)//大于一
{
int iPageSize = iNewsNumByClass / 30;
for (int i = 1; i <= iPageSize + 1; i++)
{
urlList = url + "news_list.aspx?news_classid=" + list.News_ClassId + "&page=" + i.ToString();
if (i == 1)
fpHtml = fp + "news_list" + list.News_ClassId + ".html";
else
fpHtml = fp + "news_list" + list.News_ClassId + "_" + i.ToString() + ".html";
string sContent = cmn.GetDataFromRemoteUrl(urlList);
sContent = sContent.Replace("$firstPage$", "news_list" + list.News_ClassId + "_1.html");//首页
sContent = sContent.Replace("$prePage$", "news_list" + list.News_ClassId + "_" + (i - 1).ToString() + ".html");//上一页
sContent = sContent.Replace("$nextPage$", "news_list" + list.News_ClassId + "_" + (i + 1).ToString() + ".html");//下一页
sContent = sContent.Replace("$lastPage$", "news_list" + list.News_ClassId + "_" + (iPageSize + 1).ToString() + ".html");//尾页
IList<Hope_T_Ad> listAd = Hope_T_AdManager.GetAllAd();
foreach (Hope_T_Ad list1 in listAd)
{
sContent = sContent.Replace(list1.Ad_Label, list1.Ad_Code);
}
cmn.CreateList(sContent, true, fpHtml);
Hope_T_Log objLog = new Hope_T_Log();
objLog.Log_Content = "生成文章类别" + list.News_ClassName.ToString();
objLog.Log_Time = Convert.ToDateTime(System.DateTime.Now.ToString());
objLog.Log_User = Session["HopeUser"].ToString();
Hope_T_LogManager.AddHope_T_Log(objLog);
}
}
// else
// {
urlList = url + "news_list.aspx?news_classid=" + list.News_ClassId.ToString();
fpHtml = fp + "news_list" + list.News_ClassId + ".html";
string sContentList = cmn.GetDataFromRemoteUrl(urlList);
IList<Hope_T_Ad> listAdList = Hope_T_AdManager.GetAllAd();
foreach (Hope_T_Ad list1 in listAdList)
{
sContentList = sContentList.Replace(list1.Ad_Label, list1.Ad_Code);
}
sContentList = sContentList.Replace("$firstPage$", "news_list" + list.News_ClassId + ".html");//首页
sContentList = sContentList.Replace("$prePage$", "news_list" + list.News_ClassId + ".html");//上一页
sContentList = sContentList.Replace("$nextPage$", "news_list" + list.News_ClassId + ".html");//下一页
sContentList = sContentList.Replace("$lastPage$", "news_list" + list.News_ClassId + ".html");//尾页
cmn.CreateList(sContentList, true, fpHtml);
// }
}
}
//生成全部news_info.aspx
IList<Hope_T_New> listNews = Hope_T_NewManager.GetAllHope_T_News();
StringBuilder sb = new StringBuilder();
foreach (Hope_T_New list in listNews)
{
string strUrl = url + "news_info.aspx?news_id=" + list.News_Id.ToString();
string strFilePath = fp + Convert.ToDateTime(list.News_PostTime.ToString()).ToShortDateString().Replace("-", "") + list.News_Id.ToString() + ".html";
string sContent = cmn.GetDataFromRemoteUrl(strUrl);
IList<Hope_T_Ad> listAd = Hope_T_AdManager.GetAllAd();
foreach (Hope_T_Ad list1 in listAd)
{
sContent = sContent.Replace(list1.Ad_Label, list1.Ad_Code);
}
if (cmn.CreateList(sContent, true, strFilePath))
{
Hope_T_Log objLog = new Hope_T_Log();
objLog.Log_Content = "生成全部文章";
objLog.Log_Time = Convert.ToDateTime(System.DateTime.Now.ToString());
objLog.Log_User = Session["HopeUser"].ToString();
Hope_T_LogManager.AddHope_T_Log(objLog);
Response.Write("<script>alert('已全部生成成功');location.href=location.href;</script>");
}
}
btnMakeHtml.Text = "重新生成";
btnMakeHtml.Enabled = true;
}