public async Task <ActionResult> ResolveUrl(string key) { if (string.IsNullOrEmpty(key)) { _logger.LogDebug("Url key is null or empty"); return(BadRequest()); } var url = await _urlShortenerService.GetUrlAsync(key); if (string.IsNullOrEmpty(url)) { _logger.LogDebug($"Url is empty. Url key: [{key}]"); return(NotFound()); } return(Redirect(url)); }
public async Task <ActionResult> ResolveUrl(string key) { _logger.LogDebug($"Resolving shortened url: {Request.Path.Value}"); if (string.IsNullOrEmpty(key)) { _logger.LogDebug("Url key is null or empty"); return(BadRequest()); } var url = await _urlShortenerService.GetUrlAsync(key); if (string.IsNullOrEmpty(url)) { _logger.LogDebug($"Url is empty. Url key: [{key}]"); return(NotFound()); } Request.Headers.TryGetValue("Accept", out var accept); if (!StringValues.IsNullOrEmpty(accept) && ((ICollection <string>)accept).Contains("application/json")) { var uri = new Uri(url); var message = HttpUtility.ParseQueryString(uri.Query)["m"]; if (string.IsNullOrEmpty(message)) { _logger.LogDebug("Url query param is null or empty"); return(StatusCode(StatusCodes.Status500InternalServerError)); } var jsonMessage = message.FromBase64Url(); if (string.IsNullOrEmpty(jsonMessage)) { _logger.LogDebug("JSON is null or empty"); return(StatusCode(StatusCodes.Status500InternalServerError)); } _logger.LogDebug($"Returning JSON"); Response.Headers.Add("Location", url); return(Content(jsonMessage, "application/json; charset=utf-8")); } _logger.LogDebug($"Redirecting to {url}"); return(Redirect(url)); }