日期:2014-05-17  浏览次数:20557 次

asp.net静态页模板标签的问题
由于需要。需生成静态页。
大致思路如下:
静态页模板中的标签用数据库中的字段或实体类字段、然后查找静态页中的标签。如果区配则直接替换相应的值
如下代码模板页
<html>
<head>
<title>{title}</title>
</head>
<body>
{content}
</body>
同样在表中也存在title和content字段。实体类中也存在title和content属性。在生成静态页的时候。将表中的字段读取到实体类中。然后将模板页的标签读取出来与实体类中的属性作比较。如果相同则真接替换。。。

 问题:
  1.不知道如上思路是否可行,觉得最大的好处就是不用记住那些复杂的标签了
  2.如果可行,如何去做这个比较并替换!!

------解决方案--------------------
把html源码读取到一个string里面,然后遍历数据库中的标签替换,完成后写出到html即可
------解决方案--------------------
public static bool WriteFile(string strText)
{
string path = HttpContext.Current.Server.MapPath("Html/");
System.Text.Encoding code = System.Text.Encoding.GetEncoding("gb2312");
string temp = HttpContext.Current.Server.MapPath("Html/A.html");
StreamReader sr = null; 
StreamWriter sw = null; 
string str = "";
try
{
sr = new StreamReader(temp, code);
str = sr.ReadToEnd(); 
}
catch (Exception exp)
{
HttpContext.Current.Response.Write(exp.Message);
HttpContext.Current.Response.End(); 
sr.Close();
}
string htmlfilename = DateTime.Now.ToString("yyyyMMddHHmmss") + ".html";
//通过正则获取标签内容实现内容替换
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;
}
或通过属性名称,添加到集合,再遍历替换标签
------解决方案--------------------
我现在正在做一个生成静态页面的网站.也是通过.模版中设置自定义标签.
然后.在读取数据库中的数据进行替换
------解决方案--------------------
我是用XML转的