《2022年2022年关于.net生成静态页面的方法总》由会员分享,可在线阅读,更多相关《2022年2022年关于.net生成静态页面的方法总(14页珍藏版)》请在金锄头文库上搜索。
1、1)做一个比较好的模板temp1.html,并在模板中写好题目,内容,作者以及发布日期的标记,如果还有其他列表的话也要写好其他列表的标记,如题目可以用标记:$Title$,内容可以用 $cont$,发布日期$PubDate,最新发布新闻列表$DtNewest$;2)设计数据库,可以设置两张表,一张表存放模板,一张用于存放发布新闻的内容如 模板表:TempTable : ID ,classid,TempPath(存放模板的路径 ); 新闻表:NesTable: ID,ClassID,title,cont,Filepath(发布后存放静态页面的路径) ,pubdate,author,status;
2、 3)添加新闻时,现在记录添加到新闻表中,然后再根据栏目的ID 找到该栏目的模板,把模板中的内容读取到一个字符串变量中,并用新闻表中的字段替换模板中的相应标记,然后调用c# 中的读写文件的类,写到一个静态文件中如:News.HTml,写成功后再更新数据中静态文件路径filepath; 这样一个静态文件就写好了另外本人也从网络上搜集了一些资料,供大家参考(关于新闻内容分页的情况下次叙述)一、类似的模板模板页 Text.html代码 $ShowArticle$ $title$ $author$ $content$ 二、 C# 生成静态页类代码| 支持列表生成代码using System; usin
3、g System.Data; using System.Configuration; using System.Web; using System.Web.Security; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 14 页 - - - - - - - - - using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using S
4、ystem.Web.UI.HtmlControls; using zj123.Model; using System.IO; using System.Text.RegularExpressions; using System.Runtime.InteropServices; using System.Text; using System.Collections; namespace WebUI.html public class EasyHtml public bool MakeHtml(string artid) bool succ = false; int len = Convert.T
5、oInt32(Convert.ToString(System.Configuration.ConfigurationManager.AppSettingstitlelength) ? 20); /读取数据 string sql = string.Format(select a.*,b.ClassName,b.Depth,b.OrderBy,b.isTop,b.isList,b.IsLinks,b.Links,b.PicURL,b.ParentID,b.TemplateoutId,b.TemplateinNeiId,b.TemplateinListId from article a,zone b
6、 where a.classid=b.classid and a.Status=3 and a.articleid=0, artid); DataTable dt = DbHelperOleDb.Query(sql).Tables0; if(dt.Rows.Count0) /外模板 string waitemp = ; DataTable waidt =DbHelperOleDb.Query(string.Format(select * from template where TemplateID=0,Convert.ToString(dt.Rows0TemplateoutId).Trim()
7、.Tables0; if (waidt.Rows.Count 0) waitemp = Convert.ToString(waidt.Rows0TemplateContent); /内模板 string neitemp = ; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 2 页,共 14 页 - - - - - - - - - DataTable neidt = DbHelperOleDb.Query(string.Format(select * from template wh
8、ere TemplateID=0, Convert.ToString(dt.Rows0TemplateinNeiId).Trim().Tables0; if (neidt.Rows.Count 0) neitemp = Convert.ToString(neidt.Rows0TemplateContent); / 替换模板waitemp = waitemp.Replace($intemplate$, neitemp); / 替换类型string tempclassname = classes.Common.GetP(Convert.ToString(dt.Rows0classid); wait
9、emp = waitemp.Replace($ClassName$, tempclassname); #region 文章列表 / 最新的文章System.Text.StringBuilder sbneartitle = new System.Text.StringBuilder(); DataTable nearTitledt = DbHelperOleDb.Query(select top 10 case when len(title) + len + then substring(title,1, + len + ) else title end as Title,HtmlPath fr
10、om article where iscreate=1 order by updatetime desc).Tables0; for (int i = 0; i nearTitledt.Rows.Count; i+) sbneartitle.AppendFormat(1,Convert.ToString(nearTitledt.RowsiHtmlPath),Convert.ToString(nearTitledt.RowsiTitle); sbneartitle.Append(); waitemp = waitemp.Replace($NearTitle$, sbneartitle.ToStr
11、ing(); sbneartitle.Remove(0,sbneartitle.ToString().Length); if (waitemp.Contains($OnTopTitle$) sbneartitle.Append(); / 固定nearTitledt = DbHelperOleDb.Query(select top 10 case when len(title) + len + then substring(title,1, + len + ) else title end as Title,HtmlPath from article where iscreate=1 and O
12、nTop=1 order by updatetime desc).Tables0; for (int i = 0; i nearTitledt.Rows.Count; i+) sbneartitle.AppendFormat(1, Convert.ToString(nearTitledt.RowsiHtmlPath), Convert.ToString(nearTitledt.RowsiTitle); sbneartitle.Remove(0, sbneartitle.ToString().Length); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - -
13、- - - - - - - 名师精心整理 - - - - - - - 第 3 页,共 14 页 - - - - - - - - - sbneartitle.Append(); waitemp = waitemp.Replace($OnTopTitle$, sbneartitle.ToString(); sbneartitle.Remove(0, sbneartitle.ToString().Length); if (waitemp.Contains($HitsTitle$) sbneartitle.Append(); / 热门nearTitledt = DbHelperOleDb.Query(
14、select top 10 case when len(title) + len + then substring(title,1, + len + ) else title end as Title,HtmlPath from article where iscreate=1 and Hits1000 order by Hits desc,updatetime desc).Tables0; for (int i = 0; i nearTitledt.Rows.Count; i+) sbneartitle.AppendFormat(1, Convert.ToString(nearTitledt
15、.RowsiHtmlPath), Convert.ToString(nearTitledt.RowsiTitle); sbneartitle.Append(); waitemp = waitemp.Replace($HitsTitle$, sbneartitle.ToString(); sbneartitle.Remove(0, sbneartitle.ToString().Length); if (waitemp.Contains($Elite$) sbneartitle.Append(); / 推荐 Elite nearTitledt = DbHelperOleDb.Query(selec
16、t top 10 case when len(title) + len + then substring(title,1, + len + ) else title end as Title,HtmlPath from article where iscreate=1 and Elite=1 order by updatetime desc).Tables0; for (int i = 0; i nearTitledt.Rows.Count; i+) sbneartitle.AppendFormat(1, Convert.ToString(nearTitledt.RowsiHtmlPath),
17、 Convert.ToString(nearTitledt.RowsiTitle); sbneartitle.Append(); waitemp = waitemp.Replace($Elite$, sbneartitle.ToString(); sbneartitle.Remove(0, sbneartitle.ToString().Length); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 4 页,共 14 页 - - - - - - - - - / 相关 Classid
18、if (waitemp.Contains($ClassTitle$) sbneartitle.Append(); nearTitledt = DbHelperOleDb.Query(select top 10 case when len(title) + len + then substring(title,1, + len + ) else title end as Title,HtmlPath from article where iscreate=1 and classid= + Convert.ToString(dt.Rows0classid) + order by updatetim
19、e desc).Tables0; for (int curr = 0; curr nearTitledt.Rows.Count; curr+) sbneartitle.AppendFormat(1, Convert.ToString(nearTitledt.RowscurrHtmlPath), Convert.ToString(nearTitledt.RowscurrTitle); sbneartitle.Append(); waitemp = waitemp.Replace($ClassTitle$, sbneartitle.ToString(); sbneartitle.Remove(0,
20、 sbneartitle.ToString().Length); / 相关 Classid if (waitemp.Contains($LikeTitle$) sbneartitle.Append(); DataTable dtlike = zj123.Model.Article.GetLike(artid); for (int likei = 0; likei dtlike.Rows.Count; likei+) sbneartitle.AppendFormat(1, Convert.ToString(dtlike.RowslikeiHtmlPath), Convert.ToString(d
21、tlike.RowslikeiTitle); sbneartitle.Append(); waitemp = waitemp.Replace($LikeTitle$, sbneartitle.ToString(); sbneartitle.Remove(0, sbneartitle.ToString().Length); / #endregion / 替换最新的数据for (int j = 0; j dt.Columns.Count; j+) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - -
22、- 第 5 页,共 14 页 - - - - - - - - - if (dt.Columnsj.ColumnName.Trim() = Title) string temp1 = ; string temp2 = ; string typefont = Convert.ToString(dt.Rows0TitleFontType); if (typefont = 0) temp1 = ; temp2 = ; else if (typefont = 1) temp1 = ; temp2 = ; else if (typefont = 2) temp1 = ; temp2 = ; else /S
23、ystem.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(); /Match match = Regex.Matches(); /reg.Replace(waitemp, Convert.ToString(dt.Rows0dt.Columnsj.ColumnName.Trim(), 1, waitemp.IndexOf($Title$); string temp11 = waitemp.Substring(0, waitemp.IndexOf($Title$) + 8); string
24、temp22 = waitemp.Substring(waitemp.IndexOf($Title$) + 8); temp11 = temp11.Replace($Title$, Convert.ToString(dt.Rows0dt.Columnsj.ColumnName.Trim()+-+Regex.Replace(Regex.Replace(tempclassname,*,),|*,); temp22 = temp22.Replace($Title$, + temp1 + Convert.ToString(dt.Rows0dt.Columnsj.ColumnName.Trim() +
25、temp2 + ); waitemp = temp11 + temp22; temp11 = null; temp22 = null; /waitemp = waitemp.Replace($ + dt.Columnsj.ColumnName.Trim() + $, +temp1+ 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 6 页,共 14 页 - - - - - - - - - Convert.ToString(dt.Rows0dt.Columnsj.ColumnName.T
26、rim() +temp2+ ); else if (dt.Columnsj.ColumnName.Trim().ToLower() = hits) waitemp = waitemp.Replace($ + dt.Columnsj.ColumnName.Trim() + $, Convert.ToInt16(dt.Rows0dt.Columnsj.ColumnName.Trim() 1000 ? (热门 ) : (); else if (dt.Columnsj.ColumnName.Trim().ToLower() = ontop) waitemp = waitemp.Replace($ +
27、dt.Columnsj.ColumnName.Trim() + $, Convert.ToString(dt.Rows0dt.Columnsj.ColumnName.Trim().ToLower() = true ? (置顶 ) : (); else if (dt.Columnsj.ColumnName.Trim().ToLower() = elite) waitemp = waitemp.Replace($ + dt.Columnsj.ColumnName.Trim() + $, Convert.ToString(dt.Rows0dt.Columnsj.ColumnName.Trim().T
28、oLower().ToString() = true ? (推荐 ) : (); else waitemp = waitemp.Replace($ + dt.Columnsj.ColumnName.Trim() + $, Convert.ToString(dt.Rows0dt.Columnsj.ColumnName.Trim(); / 替换链表信息/$typejs$ waitemp = waitemp.Replace($typejs$, Convert.ToString(dt.Rows0ClassID); /$numjs$ waitemp = waitemp.Replace($numjs$,
29、10); /$setjs$ waitemp = waitemp.Replace($setjs$,); /js脚本信息/ 根据路径生成页面/ 路径 检查string path = System.Web.HttpContext.Current.Request.PhysicalApplicationPath.Trim() + Convert.ToString(dt.Rows0Links) + + 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 7 页,共 14 页 - - - - - -
30、- - - Convert.ToDateTime(dt.Rows0CreateTime).ToString(yyyy-MM); if(!Directory.Exists(path) Directory.CreateDirectory(path); string filename = + Convert.ToString(dt.Rows0ArticleID) + .html; using(StreamWriter sw = new StreamWriter(path+filename,false,System.Text.Encoding.GetEncoding(gb2312) sw.Write(
31、waitemp); sw.Flush(); sw.Close(); / 修改文章转台try if (artid = null) return false; int j = int.Parse(artid); catch return false; string sql1 = string.Format(update article set iscreate=1,htmlpath=0 where articleid=1, / + Convert.ToString(dt.Rows0Links).Replace(, /) + / + Convert.ToDateTime(dt.Rows0Create
32、Time).ToString(yyyy-MM) + filename, artid); DbHelperOleDb.ExecuteSql(sql1); else return false; return succ; / / 生成列表页/ / / public bool MakeList(string classid) int len = 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 8 页,共 14 页 - - - - - - - - - Convert.ToInt32(Conve
33、rt.ToString(System.Configuration.ConfigurationManager.AppSettingstitlelength) ? 20); bool succ = false; try int i = int.Parse(classid); catch return false; DataTable dt = DbHelperOleDb.Query(string.Format(select a.*,b.ClassName,b.Depth,b.OrderBy,b.isTop,b.isList,b.IsLinks,b.Links,b.PicURL,b.ParentID
34、,b.TemplateoutlistId,b.TemplateoutId,b.TemplateinNeiId,b.TemplateinListId,b.keywords,b.description from article a,zone b where a.classid=b.classid and iscreate=1 and a.Status=3 and a.classid=0 order by a.UpdateTime desc,OnTop desc,Elite desc,Hits desc, classid).Tables0; if (dt.Rows.Count 0) string c
35、lasspath = HttpContext.Current.Request.PhysicalApplicationPath + + Convert.ToString(dt.Rows0Links).Trim(); / 读取外模板信息string templatewai = ; DataTable templatewaidt = DbHelperOleDb.Query(string.Format(select * from template where TemplateID=0, Convert.ToString(dt.Rows0TemplateoutlistId).Tables0; if (t
36、emplatewaidt.Rows.Count 0) templatewai = Convert.ToString(templatewaidt.Rows0TemplateContent); / 根据类型替换标题string tempclassname = classes.Common.GetP(classid); templatewai = templatewai.Replace($Title$, Convert.ToString(dt.Rows0ClassName).Trim() +-+Regex.Replace(Regex.Replace(tempclassname,*,),*,); te
37、mplatewai = templatewai.Replace($ClassTitleTop$, Convert.ToString(dt.Rows0ClassName).Trim(); templatewai = templatewai.Replace($ClassName$, tempclassname); templatewai = templatewai.Replace($Keyword$, Convert.ToString(dt.Rows0Keywords).Trim(); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - -
38、 名师精心整理 - - - - - - - 第 9 页,共 14 页 - - - - - - - - - templatewai = templatewai.Replace($Description$, Convert.ToString(dt.Rows0Description).Trim(); int zong = dt.Rows.Count; int size = 10; try size = Convert.ToInt16(System.Configuration.ConfigurationManager.AppSettingspagesize); catch size = 10; int
39、 pagecount = zong % size = 0 ? (zong / 10) : (zong / 10 + 1); string listcontent = ; DataTable dtlist = DbHelperOleDb.Query(string.Format(select * from template where TemplateID=0, Convert.ToString(dt.Rows0TemplateinListId).Tables0; if (dtlist.Rows.Count 0) listcontent = Convert.ToString(dtlist.Rows
40、0TemplateContent); / Match mat = Regex.Match(this.rtbconten.Text.Trim(), (?.*), RegexOptions.Multiline | RegexOptions.IgnoreCase); / MessageBox.Show(mat.Groupscontent.Value); /(?.*) / Match mat = Regex.Match(listcontent, (?.*)(?.*)(?.*)(?.*),RegexOptions.IgnoreCase|RegexOptions.Multiline); string he
41、ad = Convert.ToString(mat.Groupshead); string body = Convert.ToString(mat.Groupsbody); string page = Convert.ToString(mat.Groupspage); string bottom = Convert.ToString(mat.Groupsbottom); / 循环生成分页for (int i = 0; i pagecount; i+) /System.Text.StringBuilder sbneilist = new System.Text.StringBuilder();
42、System.Text.StringBuilder sbneilist = new System.Text.StringBuilder(head); int l = 0; for (int j = (i * size zong) ? (i * size) : (zong); j (i + 1) * size zong ? (i + 1) * size) : (zong); j+) /sbneilist.Append( ); /string temp = listcontent; string temp = body; for (int k = 0; k dt.Columns.Count; k+
43、) temp = temp.Replace($ + dt.Columnsk.ColumnName.Trim() + $, Convert.ToString(dt.Rowsjdt.Columnsk.ColumnName.Trim(); if (+l) % 5 = 0) sbneilist.Append(temp).Append(); else sbneilist.Append(temp); /sbneilist.Append(); /sbneilist.Append().Append(classes.Common.GetPage(i + 1, size, zong).Append(); sbne
44、ilist.Append(page.Replace(¥pages ¥, classes.Common.GetPage(i + 1, size, zong); / sbneilist.Append(); sbneilist.Append(bottom); #region 文章列表 / 最新的文章System.Text.StringBuilder sbneartitle = new System.Text.StringBuilder(); DataTable nearTitledt = DbHelperOleDb.Query(select top 10 case when len(title) +
45、 len + then substring(title,1, + len + ) else title end as Title,HtmlPath from 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 11 页,共 14 页 - - - - - - - - - article where iscreate=1 order by updatetime desc).Tables0; for (int curr = 0; curr nearTitledt.Rows.Count; cur
46、r+) sbneartitle.AppendFormat(1, Convert.ToString(nearTitledt.RowscurrHtmlPath), Convert.ToString(nearTitledt.RowscurrTitle); sbneartitle.Append(); templatewai = templatewai.Replace($NearTitle$, sbneartitle.ToString(); sbneartitle.Remove(0, sbneartitle.ToString().Length); sbneartitle.Append(); / 固定ne
47、arTitledt = DbHelperOleDb.Query(select top 10 case when len(title) + len + then substring(title,1, + len + ) else title end as Title,HtmlPath from article where iscreate=1 and OnTop=1 order by updatetime desc).Tables0; for (int curr = 0; curr nearTitledt.Rows.Count; curr+) sbneartitle.AppendFormat(1
48、, Convert.ToString(nearTitledt.RowscurrHtmlPath), Convert.ToString(nearTitledt.RowscurrTitle); sbneartitle.Remove(0, sbneartitle.ToString().Length); sbneartitle.Append(); templatewai = templatewai.Replace($OnTopTitle$, sbneartitle.ToString(); sbneartitle.Remove(0, sbneartitle.ToString().Length); sbn
49、eartitle.Append(); / 热门nearTitledt = DbHelperOleDb.Query(select top 10 case when len(title) + len + then substring(title,1, + len + ) else title end as Title,HtmlPath from article where iscreate=1 and Hits1000 order by Hits desc,updatetime desc).Tables0; for (int curr = 0; curr nearTitledt.Rows.Coun
50、t; curr+) sbneartitle.AppendFormat(1, Convert.ToString(nearTitledt.RowscurrHtmlPath), Convert.ToString(nearTitledt.RowscurrTitle); sbneartitle.Append(); templatewai = templatewai.Replace($HitsTitle$, sbneartitle.ToString(); sbneartitle.Remove(0, sbneartitle.ToString().Length); sbneartitle.Append();
51、/ 推荐 Elite 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 12 页,共 14 页 - - - - - - - - - nearTitledt = DbHelperOleDb.Query(select top 10 case when len(title) + len + then substring(title,1, + len + ) else title end as Title,HtmlPath from article where iscreate=1 and E
52、lite=1 order by updatetime desc).Tables0; for (int curr = 0; curr nearTitledt.Rows.Count; curr+) sbneartitle.AppendFormat(1, Convert.ToString(nearTitledt.RowscurrHtmlPath), Convert.ToString(nearTitledt.RowscurrTitle); sbneartitle.Remove(0, sbneartitle.ToString().Length); sbneartitle.Append(); templa
53、tewai = templatewai.Replace($Elite$, sbneartitle.ToString(); sbneartitle.Append(); / 相关 Classid nearTitledt = DbHelperOleDb.Query(select top 10 case when len(title) + len + then substring(title,1, + len + ) else title end as Title,HtmlPath from article where iscreate=1 and classid= + classid + order
54、 by updatetime desc).Tables0; for (int curr = 0; curr nearTitledt.Rows.Count; curr+) sbneartitle.AppendFormat(1, Convert.ToString(nearTitledt.RowscurrHtmlPath), Convert.ToString(nearTitledt.RowscurrTitle); sbneartitle.Append(); templatewai = templatewai.Replace($ClassTitle$, sbneartitle.ToString();
55、sbneartitle.Remove(0, sbneartitle.ToString().Length); /sbneartitle.Append(); /DataTable dtlike = zj123.Model.Article.GetLike(artid); /for (int likei = 0; likei dtlike.Rows.Count; likei+) / / sbneartitle.AppendFormat(1, Convert.ToString(dtlike.RowslikeiHtmlPath), Convert.ToString(dtlike.RowslikeiTitl
56、e); / /sbneartitle.Append(); /waitemp = waitemp.Replace($LikeTitle$, sbneartitle.ToString(); /sbneartitle.Remove(0, sbneartitle.ToString().Length); / #endregion / 替换链表信息/$typejs$ 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 13 页,共 14 页 - - - - - - - - - templatewai
57、 = templatewai.Replace($typejs$, classid); /$numjs$ templatewai = templatewai.Replace($numjs$, 10); /$setjs$ templatewai = templatewai.Replace($setjs$, ); string tempzong = templatewai.Replace($intemplate$, sbneilist.ToString(); string indexshow = (i + 1).ToString() = 1 ? () : (i + 1).ToString(); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 14 页,共 14 页 - - - - - - - - -