public HttpResponseMessage Get([FromUri] string key) { try { var business = new UrlShortenerBusiness(); var longUrl = business.GetLongUrl(key); if (string.IsNullOrEmpty(longUrl)) { return(Request.CreateResponse(HttpStatusCode.NotFound)); } var response = Request.CreateResponse(HttpStatusCode.Moved); if (Uri.IsWellFormedUriString(longUrl, UriKind.Absolute)) { response.Headers.Location = new Uri(longUrl); } else { response.Headers.Location = new Uri("http://" + longUrl); } return(response); } catch (Exception) { return(Request.CreateResponse(HttpStatusCode.NotFound)); } }
public JsonResult Generate(UrlViewModel url) { if (ModelState.IsValid) { var urlModel = new Url(); urlModel.LongUrl = url.LongUrl; urlModel.ShortUrl = Request.Url.Scheme + "://" + Request.Url.Authority + "/"; var business = new UrlShortenerBusiness(); var result = business.GenerateShortUrl(urlModel); return(Json(result)); } else { return(Json(null)); } }