目前asp.net常见webform中生成静态页面为伪静态或用后台代码生成。
1、伪静态也就是利用MS的URLRewriter.dll,进行URL的重写(这个我不常用,因为配置很多,很死板)
2、通过后台code实现
其实针对后台代码生成静态页面,其实很简单,原理可能都想的到,就是文件的创建、再创建,替换、再替换,也就是说我们将我们生成后的aspx文件替换成我们想要的.html或htm,但这样其实对于开发前端很难控制,也很麻烦。因此在我生成静态页面的时候,首先是做模板,做要生成静态页面的模板,用特殊符号代替需要动态变化的标签:
如:我们一个网站标头,您可以用以下代码作为替换标识
<title>{$htmlTitle}</title>
<meta name="keywords" content="{$htmlKeyWord}" />
<meta name="describle" content="{$htmlDescrible}" />
以上代码中的带有{$$}的是进行后台替换的标识
后台定义模板code:
//定义模板
Array arryTemplete = Array.CreateInstance(typeof(System.String), 5);
arryTemplete.SetValue("templete1", 0);
arryTemplete.SetValue("templete2", 1);
//定位自定义模板
Array arryTempleteList = Array.CreateInstance(typeof(System.String), 5);
arryTempleteList.SetValue("$index.html", 0);
arryTempleteList.SetValue("$content.html", 1);
arryTempleteList.SetValue("$list.html", 2);
arryTempleteList.SetValue("$detail.html", 3);
arryTempleteList.SetValue("$product.html", 4);
模板关键字替换code:
private Dictionary<string, string> CreateHtml(HttpContext content, string templeteUrl, int indexShow, Menue menueMode)
{
Dictionary<string, string> dic = new Dictionary<string, string>();
string strHtml = CreatePageHtml(content, templeteUrl);
string strMenue = string.Empty;
//公共HTML
DataTable dt = new ConfigBLL().GetList("1");
dic.Add("{$htmlTitle$}", dt.Rows[0]["F_WebSiteName"].ToString());
dic.Add("{$htmlKeyWord$}", dt.Rows[0]["F_KeyWord"].ToString());
dic.Add("{$htmlDescrible$}", dt.Rows[0]["F_Describle"].ToString());
dic.Add("{$htmlLogo$}", dt.Rows[0]["F_Logo"].ToString());
dic.Add("{$htmlbanner$}", dt.Rows[0]["F_Banner"].ToString());
dic.Add("{$htmlbannerdescrible$}", dt.Rows[0]["F_BannerDescrible"].ToString());
dic.Add("{$htmlfooter$}", dt.Rows[0]["F_Footer"].ToString());
//菜单HTML
Menue mod = new Menue();
dt = new MenueBLL().GetMenue(mod);
for (int i = 0; i < dt.Rows.Count; i++)
{
dic.Add("{$htmlmenue_" + (i + 1) + "$}", dt.Rows[i]["f_name"].ToString());
dic.Add("{$htmlmenueurl_" + (i + 1) + "$}", dt.Rows[i]["f_url"].ToString());
}
//服务数据
Modal.Service modService = new Modal.Service();
modService.f_index = indexShow;
dt = new ServiceBLL().GetList(modService);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < dt.Rows.Count; i++)
{
dic.Add("{$htmlServiceName_" + (i + 1) + "$}", dt.Rows[i]["F_NAME"].ToString());
if (dt.Rows[i]["F_DESCRIBLE"].ToString().Length > 80)
{
dic.Add("{$htmlServiceDescrible_" + (i + 1) + "$}", dt.Rows[i]["F_DESCRIBLE"].ToString().Substring(0, 80) + "...");
}
else
{
dic.Add("{$htmlServiceDescrible_" + (i + 1) + "$}", dt.Rows[i]["F_DESCRIBLE"].ToString());
}
}
//图片数据
Modal.Image modImg = new Modal.Image();
modImg.f_index = indexShow;
dt = new ImageBLL().GetList(modImg);
for (int i = 0; i < dt.Rows.Count; i++)
{
dic.Add("{$htmlimg_" + (i + 1) + "$}", dt.Rows[i]["F_URL"].ToString());
dic.Add("{$htmlimgname_" + (i + 1) + "$}", dt.Rows[i]["F_NAME"].ToString());
}
//菜单对应的标题
dic.Add("{$htmlContentTitle$}", menueMode.f_name);
dic.Add("{$htmlContent$}", MakeContent(menueMode.fid));
return dic;
}
通过模板生成静态页面(文件创建操作):
/// <summary>
/// 生成HTML文件
/// </summary>
/// <param name="context"></param>
/// <param name="templeteUrl">模板文件的的路径</param>
/// <param name="dic">替换文件</param>
/// <param name="fileName">生成的文件名</param>
/// <returns></returns>
public string CreatePageHtml(HttpContext context, string templeteUrl, Dictionary<string, string> dic, string fileName)
{
//----将html模版读入StringBuilder对象里面
string result = string.Empty; ;
StringBuilder htmltext = new StringBuilder();
try
{
using (StreamReader sr = new StreamReader(context.Server.MapPath("../../Templete/" + templete + "/" + templeteUrl)))
{
string line;
while ((line = sr.ReadLine()) != null)
{
htmltext.Append(line);
}
sr.Close();
}
}
catch
{
}
//----------替换模板文件--------------------
foreach (KeyValuePair<string, string> pair in dic)
{
htmltext.Replace(pair.Key, pair.Value);
}
//----------生成html文件------------------――
try
{
using (StreamWriter sw = new StreamWriter(context.Server.MapPath(fileName), false, System.Text.Encoding.GetEncoding("UTF-8")))
{
result = "1";
sw.WriteLine(htmltext);
sw.Flush();
sw.Close();
}
}
catch
{
}
return result;
}