图书5
在BLL里面
1 /// 将商品的信息生成静态页面 2 /// </summary> 3 public void CreateHtmlPage(int id) 4 { 5 Model.Book model=dal.GetModel(id); 6 //获取模板文件 7 string template = HttpContext.Current.Request.MapPath("/Template/BookTemplate.html"); 8 string fileContent = File.ReadAllText(template); 9 fileContent = fileContent.Replace("$title", model.Title).Replace("$author", model.Author).Replace("$unitprice",model.UnitPrice.ToString("0.00")).Replace("$isbn",model.ISBN).Replace("$content",model.ContentDescription).Replace("$bookId",model.Id.ToString()); 10 string dir = "/HtmlPage/" + model.PublishDate.Year + "/" + model.PublishDate.Month + "/" + model.PublishDate.Day + "/"; 11 Directory.CreateDirectory(Path.GetDirectoryName(HttpContext.Current.Request.MapPath(dir))); 12 string fullDir = dir + model.Id + ".html"; 13 File.WriteAllText(HttpContext.Current.Request.MapPath(fullDir), fileContent, System.Text.Encoding.UTF8); 14 15 }
在服务器端静态页面
将商品的详细信息页面建成静态页面。
静态页面好处:(1).访问速度快(2):减轻数据库的压力。所以是网站优化中很重要的手段。
劣势:
什么样的内容适合做成静态页面?
经常访问的页面,但是页面的内容不是经常修改的。
1 public partial class CreateStaticPage : System.Web.UI.Page 2 { 3 protected void Page_Load(object sender, EventArgs e) 4 { 5 if (IsPostBack) 6 { 7 BLL.BookManager bll = new BLL.BookManager(); 8 List<Model.Book> list = bll.GetModelList(""); 9 foreach (Model.Book bookModel in list) 10 { 11 bll.CreateHtmlPage(bookModel.Id); 12 } 13 } 14 } 15 }
评论
1 public class BookComment : IHttpHandler 2 { 3 BLL.BookCommentManager bll = new BLL.BookCommentManager(); 4 BLL.Articel_WordsManager articelManager = new BLL.Articel_WordsManager(); 5 public void ProcessRequest(HttpContext context) 6 { 7 context.Response.ContentType = "text/plain"; 8 string action = context.Request["action"]; 9 if (action == "add") 10 { 11 AddComment(context);//添加评论 12 } 13 else if (action == "load")//加载评论 14 { 15 LoadComment(context); 16 } 17 else 18 { 19 context.Response.Write("参数错误!!"); 20 } 21 } 22 /// <summary> 23 /// 加载评论 24 /// </summary> 25 private void LoadComment(HttpContext context) 26 { 27 int bookId = Convert.ToInt32(context.Request["bookId"]); 28 List<Model.BookComment>list=bll.GetModelList("BookId="+bookId);//获取指定书下面的评论 29 List<BookCommentViewModel> newList = new List<BookCommentViewModel>(); 30 foreach (Model.BookComment bookComment in list) 31 { 32 ViewModel.BookCommentViewModel viewModel = new BookCommentViewModel(); 33 viewModel.Msg =Common.WebCommon.UbbToHtml(bookComment.Msg);//将UBB编码转成HTML编码 34 TimeSpan ts=DateTime.Now-bookComment.CreateDateTime; 35 viewModel.CreateDateTime = Common.WebCommon.GetTimeSpan(ts);//获取评论的时间. 36 newList.Add(viewModel); 37 } 38 System.Web.Script.Serialization.JavaScriptSerializer js = new System.Web.Script.Serialization.JavaScriptSerializer(); 39 context.Response.Write(js.Serialize(newList)); 40 } 41 /// <summary> 42 /// 添加评论 43 /// </summary> 44 /// <param name="context"></param> 45 private void AddComment(HttpContext context) 46 { 47 48 string msg=context.Request["msg"];//接收用户发布的评论 49 //判断是否含有禁用词 50 if (articelManager.CheckForbid(msg)) 51 { 52 context.Response.Write("no:评论中含有禁用词"); 53 } 54 else if (articelManager.CheckMod(msg))//表示审查词 55 { 56 AddMsg(context, msg, 0); 57 58 } 59 else//替换词 60 { 61 AddMsg(context, msg, 1); 62 } 63 } 64 /// <summary> 65 /// 添加评论 66 /// </summary> 67 /// <param name="context"></param> 68 /// <param name="msg"></param> 69 /// <param name="isPass"></param> 70 71 private void AddMsg(HttpContext context,string msg,int isPass) 72 { 73 Model.BookComment bookComment = new Model.BookComment(); 74 bookComment.BookId = Convert.ToInt32(context.Request["bookId"]); 75 bookComment.Msg = articelManager.CheckReplace(msg);//替换词 76 bookComment.CreateDateTime = DateTime.Now; 77 //bookComment.IsPass=isPass 78 //bookComment.UserId=userId. 79 80 if (bll.Add(bookComment) > 0) 81 { 82 if (isPass == 1) 83 { 84 context.Response.Write("ok:评论成功"); 85 } 86 else 87 { 88 context.Response.Write("ok:评论成功含有审查词"); 89 } 90 } 91 else 92 { 93 context.Response.Write("no:评论失败"); 94 } 95 } 96 97 98 99 public bool IsReusable 100 { 101 get 102 { 103 return false; 104 } 105 } 106 }