.net简单的静态页生成

 

1.得到实体对象model,读取模板

string htmlMaster = File.ReadAllText(HttpContext.Current.Server.MapPath("/master/bookDetaile.htm"));
2.然后替换占位符
string repHtml = htmlMaster.Replace("$title", model.Title).Replace("$coverImage", "/Images/BookCovers/" + model.ISBN + ".jpg").Replace("$Author", model.Author).Replace("$bookDes", model.AurhorDescription).Replace("$bookPrice", model.UnitPrice.ToString()).Replace("$bookPublishDate", model.PublishDate.ToString()).Replace("$bookId", model.Id.ToString());

3.设定保存路径,注意命名规则
string path =HttpContext.Current.Server.MapPath(string.Format("/AutoCreatStaticPage/{0}/{1}/{2}/{3}.html", model.PublishDate.Year.ToString(), model.PublishDate.Month.ToString(), model.PublishDate.Day.ToString(),model.Id));
4.创建文件夹
Directory.CreateDirectory(Path.GetDirectoryName(path));
5.将字符串保存到磁盘
File.WriteAllText(path, repHtml,Encoding.UTF8);

.net简单的静态页生成,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。