public async Task <IActionResult> OnPostAsync() { InputUrl = InputUrl.Trim().ToLower(); string hash = Functions.GenerateRandomHash(5); var existingUrl = await _urlRepo.GetUrlByOriginalUrl(hash); do { hash = Functions.GenerateRandomHash(5); var exits = await _urlRepo.GetUrlByShortUrl(hash); if (exits == null) { break; } } while (existingUrl != null && hash == existingUrl.ShortUrl); if (!ModelState.IsValid) { Message = "Something went wrong, please try again"; return(Page()); } else if (!Functions.CheckValidUrl(InputUrl)) { Message = "Something went wrong, your url is not valid and could not be validated before being posted to the server. Please try again."; _log.LogWarning("Javascript validation failed."); return(Page()); } newUrl.ShortUrl = hash; newUrl.OriginalUrl = InputUrl; newUrl.CreatedDate = DateTime.Now; newUrl.Counter = 0; newUrl.IP = _accessor.HttpContext.Connection.RemoteIpAddress.ToString(); await _urlRepo.AddUrl(newUrl); return(RedirectToPage("/Index")); }
public async Task <IActionResult> s(string hash) { theUrl = await _urlRepo.GetUrlByShortUrl(hash); if (theUrl == null) { _log.LogWarning($"Redirect failed. The url could not be found."); return(Content($"Redirect failed. The url could not be found.")); } theUrl.Counter = theUrl.Counter + 1; theUrl.LastAccessed = DateTime.Now; await _urlRepo.UpdateUrl(theUrl.Id, theUrl); if (!theUrl.OriginalUrl.Contains("http://") || !theUrl.OriginalUrl.Contains("https://")) { return(Redirect("http://" + theUrl.OriginalUrl)); } return(Redirect(theUrl.OriginalUrl)); }