protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { if (Request.QueryString["bid"] != null) { int id = Convert.ToInt32(Request.QueryString["bid"]); BookManager manager = new BookManager(); manager.AddClick(id); Book book = manager.GetBookById(id); this.lblAuthor.Text = book.Author; this.lblBookName.Text = book.Title; this.lblPublisher.Text = book.Publisher.Name; this.lblCategory.Text = book.Category.Name; this.lblISBN.Text = book.ISBN; this.lblPublishDate.Text = book.PublishDate.ToShortDateString(); this.lblPrice.Text = StringUtility.ToMoney(book.UnitPrice); this.lblContent.Text = book.ContentDescription; this.lblTOC.Text = book.TOC; this.imgBook.ImageUrl = StringUtility.CoverUrl(book.ISBN); } } }
// // GET: /Book/ public ActionResult Detail() { int id = RouteData.Values["id"] != null?Convert.ToInt32(RouteData.Values["id"]) : 0; if (id > 0) { BookManager manager = new BookManager(); manager.AddClick(id); Book book = manager.GetBookById(id); return(View(book)); } return(View()); }
public void ProcessRequest(HttpContext context) { //由地址获取bookId int i = context.Request.Path.LastIndexOf("/"); string strBookId = context.Request.Path.Substring(i + 1, context.Request.Path.Length - (i + 6)); int bookId = Convert.ToInt32(strBookId); //生成实际路径 string filePath = context.Server.MapPath("~/BookDetails/Book_" + bookId + ".html"); BookManager manager = new BookManager(); context.Application.Lock();//加锁 if (!File.Exists(filePath)) { string templatePath = context.Server.MapPath("~/BookDetails/template.html"); //模板路径 string template = ""; //模板内容 using (StreamReader sr = new StreamReader(templatePath)) { template = sr.ReadToEnd();//读取模板内容 } Book book = manager.GetBookById(bookId); //获取Book信息 /*模板内容替换*/ string html = template.Replace("[WebRootPath]", context.Request.ApplicationPath) //替换路径 .Replace("[BookId]", bookId.ToString()) .Replace("[Author]", book.Author) .Replace("[Title]", book.Title) .Replace("[PublishName]", book.Publisher.Name) .Replace("[BookCategory]", book.Category.Name) .Replace("[ISBN]", book.ISBN) .Replace("[PublishDate]", book.PublishDate.ToShortDateString()) .Replace("[UnitPrice]", StringUtility.ToMoney(book.UnitPrice)) .Replace("[ContentDescription]", book.ContentDescription) .Replace("[TOC]", book.TOC); using (StreamWriter sw = new StreamWriter(filePath)) { sw.Write(html);//写入静态化页面内容 } } context.Application.UnLock(); //解锁 manager.AddClick(bookId); //增加点击数 context.Server.Execute("~/bookDetails/Book_" + bookId + ".html"); //转到实际路径 }