public IActionResult DeleteRoute(string name, int?currentArticleId) { bool isAjax = HttpContext.Request.Headers["X-Requested-With"] == "XMLHttpRequest"; try { // Both parameters has to be filled in if (string.IsNullOrEmpty(name) || currentArticleId == null) { TempData["ErrorMessage"] = _localizer["The item could not be deleted."]; return(RedirectToAction("Index")); } else { // Let's test if article and URL exists try { Article article = dbHelper.GetArticleById((int)currentArticleId); UrlRewrite rule = dbHelper.GetAllUrls().Where(url => url.Url == name).Single(); } catch { TempData["ErrorMessage"] = _localizer["The item could not be deleted."]; return(RedirectToAction("Index")); } } // At least one ULR rewriting rule has to exist and URL rewriting has to be allowed int numberOfRules = dbHelper.GetUrls((int)currentArticleId).Count(); if (numberOfRules < 2 && Configuration.WebsiteConfig.UrlRewriting) { throw new NoUrlRulesException("At least one rewriting rule has to be present."); } dbHelper.DeleteUrl(name); TempData["Success"] = true; } catch (InvalidUrlRewriteException) { TempData["ErrorMessage"] = _localizer["The required URL was not found to be deleted."]; } catch (NoUrlRulesException) { TempData["ErrorMessage"] = _localizer["There has to be at least one URL for a article."]; } catch (Exception) { TempData["ErrorMessage"] = _localizer["The item could not be deleted."]; } if (isAjax) { // Return URL routes only for current article UrlListViewModel model = new UrlListViewModel(); model.Urls = dbHelper.GetUrls((int)currentArticleId); model.ArticleId = (int)currentArticleId; return(PartialView("UrlListPartialView", model)); } else { return(RedirectToAction("Index")); } }
public IActionResult PostArticle(EditArticleViewModel model) { bool isAjax = HttpContext.Request.Headers["X-Requested-With"] == "XMLHttpRequest"; try { if (ModelState.IsValid) { Article newArticleData = new Article(); newArticleData.Id = model.Id; newArticleData.HtmlContent = model.HtmlContent; newArticleData.Name = model.Name; newArticleData.PublishDate = DateTime.Parse(model.PublishDate); newArticleData.CategoryId = model.SelectedCategoryId; newArticleData.Publish = model.Publish; newArticleData.Keywords = model.Keywords; Article articleReference = dbHelper.EditArticle(newArticleData); // URL rewriting if (!string.IsNullOrEmpty(model.Url)) { dbHelper.SetUrl(articleReference, model.Url); } // At least one ULR rewriting rule has to exist and URL rewriting has to be allowed int numberOfRules = dbHelper.GetUrls(model.Id).Count(); if (numberOfRules < 1 && Configuration.WebsiteConfig.UrlRewriting) { throw new NoUrlRulesException("At least one rewriting rule has to be present."); } TempData["Success"] = true; } else { throw new Exception(); } } catch (NoUrlRulesException) { TempData["ErrorMessage"] = _localizer["There has to be at least one URL address."]; } catch (InvalidUrlRewriteException ex) { try { string alreadyAssignedAricleName = dbHelper.GetArticleById(ex.AssignedArticleId).Name; TempData["ErrorMessage"] = _localizer["The URL adress has already an article assigned:"] + " " + alreadyAssignedAricleName + "."; } catch { TempData["ErrorMessage"] = _localizer["The URL adress is already in use."]; } } catch (Exception) { TempData["ErrorMessage"] = _localizer["The changes could not be saved."]; } if (isAjax) { // return Content(string.Empty); // Return URL routes only for current article UrlListViewModel partialModel = new UrlListViewModel(); partialModel.Urls = dbHelper.GetAllUrls().Where(url => url.ArticleId == model.Id).Select(u => u.Url).ToList(); partialModel.ArticleId = model.Id; return(PartialView("UrlListPartialView", partialModel)); } else { return(RedirectToAction("GetArticle", new { id = model.Id })); } }