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);

posted on 2014-03-07 12:05  芝麻的西瓜  阅读(224)  评论(0编辑  收藏  举报