public void CreateStaticPages(long houseId) { HouseIndexModel model = new HouseIndexModel { Attachments = AttachmentService.GetById(houseId), House = HouseService.GetById(houseId), HousePics = HouseService.GetPics(houseId) }; var html = MVCHelper.RenderViewToString(this.ControllerContext, "~/Views/House/StaticIndex.cshtml", model); System.IO.File.WriteAllText(@"F:\code2020\asp.net-C-\asp.net基础\rentHouse\RentHouse\RentHouse\staticPages\" + houseId + ".html", html); }
public ActionResult Index(long id) { #region 未使用缓存 //var house = HouseService.GetById(id); //if (house==null) //{ // //return Json(new AjaxResult() {Status = "error", ErrorMsg = "房源获取失败"}); 这里写这个干嘛,是url又不是ajax,应该直接返回error页面 // return View("Error",(object)"不存在的房源id"); //} //var housePics = HouseService.GetPics(house.Id); //var attachments = AttachmentService.GetById(house.Id); //HouseIndexModel model = new HouseIndexModel() //{ // Attachments = attachments,House = house,HousePics = housePics //}; #endregion //使用asp.net内置缓存 #region 使用asp.net内置缓存 string cacheKey = "HouseIndex_" + id; //先尝试去缓存中找 HouseIndexModel model = (HouseIndexModel)HttpContext.Cache[cacheKey]; if (model == null) { var house = HouseService.GetById(id); if (house == null) { //return Json(new AjaxResult() {Status = "error", ErrorMsg = "房源获取失败"}); 这里写这个干嘛,是url又不是ajax,应该直接返回error页面 return(View("Error", (object)"不存在的房源id")); } var housePics = HouseService.GetPics(house.Id); var attachments = AttachmentService.GetById(house.Id); model = new HouseIndexModel() { Attachments = attachments, House = house, HousePics = housePics }; //存入缓存 HttpContext.Cache.Insert(cacheKey, model, null, DateTime.Now.AddMinutes(1), TimeSpan.Zero); } #endregion //使用redis缓存 ,wsl中的redis删掉了,先不install了,反正房源页面改为静态化,不会访问这个action了 #region redis //string cacheKey = "HouseIndex_" + id; ////先尝试去缓存中找 //HouseIndexModel model = RedisHelper.GetValue<HouseIndexModel>(cacheKey); //if (model == null) //{ // var house = HouseService.GetById(id); // if (house == null) // { // //return Json(new AjaxResult() {Status = "error", ErrorMsg = "房源获取失败"}); 这里写这个干嘛,是url又不是ajax,应该直接返回error页面 // return View("Error", (object)"不存在的房源id"); // } // var housePics = HouseService.GetPics(house.Id); // var attachments = AttachmentService.GetById(house.Id); // model = new HouseIndexModel() // { // Attachments = attachments, // House = house, // HousePics = housePics // }; // //存入缓存 // RedisHelper.SetValue(cacheKey,model,TimeSpan.FromMinutes(1)); //} #endregion return(View(model)); }