asp.net生成静态页面
1 public class HtmlGengerate 2 { 3 public void GengerateHtml() 4 { 5 // 声明控件的正则表达式 6 Regex regRepeat = new Regex("<!--Repeat(.*)RepeatEnd-->", RegexOptions.IgnoreCase); 7 8 // 声明替换的正则表达式 9 Regex regReplaceBlank = new Regex(">(\\s+)<", RegexOptions.IgnoreCase); 10 Regex regReplaceLine = new Regex("\\s\\n", RegexOptions.IgnoreCase); 11 12 // 取根目录 13 string strRoot = HttpContext.Current.Request.PhysicalApplicationPath; 14 15 // 声明模板文件源码字符串 16 string strStatic = regReplaceBlank.Replace(regReplaceLine.Replace(File.ReadAllText(strRoot + "Static.htm", System.Text.Encoding.UTF8), ""), "><"); 17 string strStaticDetail = regReplaceBlank.Replace(regReplaceLine.Replace(File.ReadAllText(strRoot + "StaticDetail.htm", System.Text.Encoding.UTF8), ""), "><"); 18 19 // 生成列表页 20 Match mRepeat = regRepeat.Match(strStatic); 21 if (mRepeat.Success) 22 { 23 // 替换Repeat控件 24 strStatic = strStatic.Replace(mRepeat.Value, GengerateRepeat(mRepeat.Value)); 25 26 // 生成StaticMain.htm 27 File.WriteAllText(strRoot + "StaticMain.htm", strStatic, System.Text.Encoding.UTF8); 28 } 29 30 // 生成详细页 31 // 获取数据源 32 DataSet ds = OpOleDb.GetDataSet(OpOleDb.connStr, "select * from Test", null); 33 34 foreach (DataRow dr in ds.Tables[0].Rows) 35 { 36 // 声明一个新的字符串,用来替换数据标签并最终生成文件 37 string strTmpDetail = strStaticDetail; 38 39 // 循环替换标签 40 foreach (DataColumn dc in ds.Tables[0].Columns) 41 { 42 strTmpDetail = strTmpDetail.Replace("{" + dc.ColumnName + "}", dr[dc.ColumnName].ToString()); 43 } 44 45 // 生成文件 46 File.WriteAllText(strRoot + "StaticDetail_" + dr["Id"].ToString() + ".htm", strTmpDetail, System.Text.Encoding.UTF8); 47 } 48 } 49 50 private string GengerateRepeat(string source) 51 { 52 string html = ""; 53 54 // 声明strStratTag和strEndTag 55 string strStartTag = "<!--Repeat{"; 56 string strEndTag = "<!--RepeatEnd-->"; 57 string strSql = ""; 58 59 // 替换title的正则表达式,用于提取控件{}内的sql查询字符串 60 Regex regReplaceTitle = new Regex("<!--(\\w{3,7}){(.*)}-->", RegexOptions.IgnoreCase); 61 62 // 提取strSql,并将source的值去掉标签部分 63 Match mTitle = regReplaceTitle.Match(source); 64 if (mTitle.Success) 65 { 66 strSql = mTitle.Value.Replace(strStartTag, "").Replace("}-->", ""); 67 source = source.Replace(mTitle.Value, "").Replace(strEndTag, ""); 68 } 69 70 // 取数据源 71 if (strSql != "") 72 { 73 DataSet ds = OpOleDb.GetDataSet(OpOleDb.connStr, strSql, null); 74 if (ds.Tables.Count > 0) 75 { 76 if (ds.Tables[0].Rows.Count > 0) 77 { 78 foreach (DataRow dr in ds.Tables[0].Rows) 79 { 80 // 声明一个临时字符串,用于保存替换过后的值 81 string strTmp = source; 82 83 // 循环替换临时字符串中的数据标签 84 foreach (DataColumn dc in ds.Tables[0].Columns) 85 { 86 strTmp = strTmp.Replace("{" + dc.ColumnName + "}",dr[dc.ColumnName].ToString()); 87 } 88 89 // 将替换过数据标签的字符串添加到需要返回的字符串中 90 html += strTmp; 91 } 92 } 93 } 94 else 95 { 96 html = "暂无数据!"; 97 } 98 } 99 else 100 { 101 html = "该控件没有配置Sql查询字符串!"; 102 } 103 104 return html; 105 } 106 }
模板列表:
1 <table cellpadding="0" cellspacing="1" style="width:200px; background:#666; color:#333;" > 2 <tr> 3 <td style="width:30px; color:#fff">ID</td> 4 <td style="width:170px; color:#fff">Name</td> 5 </tr> 6 <!--Repeat{select * from Test}--> 7 <tr style="line-height:26px; background:#fff;"> 8 <td>{Id}</td> 9 <td><a href="StaticDetail_{Id}.htm" target="_blank">{Name}</a></td> 10 </tr> 11 <!--RepeatEnd--> 12 </table>
详细页面:
1 <ul>
2 <li>ID:{Id}</li>
3 <li>Name:{Name}</li>
4 <li>Content:{Content}</li>
5 </ul>
调用:
HtmlGengerate g = new HtmlGengerate();
g.GengerateHtml();