public IActionResult Add(Link link) { ViewBag.AppUrl = _config.GetSection("AppUrl").Value; if (ModelState.IsValid && Uri.IsWellFormedUriString(link.LinkUrl, UriKind.Absolute)) { _context.Links.Add(link); try { _context.SaveChanges(); } catch (DbUpdateException e) { if (e.InnerException.Message.Contains("UNIQUE")) { ModelState.AddModelError("Key", "The Link Key must be unique"); return View(link); } throw; } return RedirectToAction("Index"); } return View(link); }
private async void IncrementLinkClickCount(Link link) { bool saveFailed; do { saveFailed = false; ++link.ClickCount; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException e) { saveFailed = true; link = _context.Links.Where(l => l.Id == link.Id).SingleOrDefault(); } } while (saveFailed); }