public ActionResult AutoSave(PageModel model) { try { if (!Request.IsAjaxRequest()) throw new InvalidOperationException(); if (Session["AdminAuthed"] == null) throw new UnauthorizedAccessException(); //load the configuration var configuration = getConfiguration(false); if (ModelState.IsValid) { //update the page configuration with the posted information var page = configuration.Pages.FirstOrDefault(x => x.Name == model.Name); if (page == null) { page = new CmsPage(); configuration.Pages.Add(page); } page.Title = model.Title; page.Order = model.Order; page.IsDraft = model.IsDraft; updateConfiguration(configuration); var pagePath = String.Format(_pageDraftPathFormatter, model.Name); //remove the out of date page, if it exist if (_File.Exists(pagePath)) _File.Delete(pagePath); //write the update page _File.WriteAllText(pagePath, model.Content); //everything worked return Content("Success"); } return Content("Invalid Item"); } catch { Response.TrySkipIisCustomErrors = true; Response.StatusCode = 500; return Content("An error auto-saving page"); } }
public ActionResult Page(PageModel model) { if (Session["AdminAuthed"] == null) return RedirectToAction("Index"); //load the configuration var configuration = getConfiguration(); if (ModelState.IsValid) { var pagePath = String.Format(_pagePathFormatter, model.Name); var pageDraftPath = String.Format(_pageDraftPathFormatter, model.Name); //get the page, if it doesn't exist create it var page = configuration.Pages.FirstOrDefault(x => x.Name == model.Name); if (page == null) { page = new CmsPage(); configuration.Pages.Add(page); } //update the page object page.Title = model.Title; page.Order = model.Order; page.IsDraft = model.IsDraft; page.IsPublished = _File.Exists(pagePath); //update configuration updateConfiguration(configuration); //determine if the page is being saved as a draft or publish if (page.IsDraft) pagePath = pageDraftPath; //remove the out of date page if it exist if (_File.Exists(pagePath)) _File.Delete(pagePath); //write the new page _File.WriteAllText(pagePath, model.Content); //if it's not a draft it's being published delete existing draft if (!page.IsDraft && _File.Exists(pageDraftPath)) _File.Delete(pageDraftPath); //everything worked go back to the pages list return RedirectToAction("Pages"); } return View(model); }