public async Task <IActionResult> Create([Bind("Id,FullName")] Url url) { if (ModelState.IsValid) { url.FullName = UrlHelpers.AppendProtocol(url.FullName); var innerUrl = await _context.Urls.FirstOrDefaultAsync(dbUrl => dbUrl.FullName == url.FullName); if (innerUrl == null) { url.ShortName = UrlHelpers.ShortenUrl(url.FullName); _context.Add(url); await _context.SaveChangesAsync(); } return(RedirectToAction(nameof(Index))); } return(View(url)); }
public async Task <IActionResult> Edit(int id, [Bind("Id,FullName")] Url url) { if (id != url.Id) { return(NotFound()); } if (ModelState.IsValid) { try { var prevUrl = await _context.Urls.FindAsync(id); _context.Entry(prevUrl).State = EntityState.Detached; url.FullName = UrlHelpers.AppendProtocol(url.FullName); if (prevUrl.FullName != url.FullName) { url.ShortName = UrlHelpers.ShortenUrl(url.FullName); _context.Update(url); await _context.SaveChangesAsync(); } } catch (DbUpdateConcurrencyException) { if (!UrlExists(url.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(url)); }