public IActionResult Long(int ID) { URLViewModel shortenView = new URLViewModel(); shortenView.ID = ID; return(View(shortenView)); }
public IActionResult Index(UserViewModel user) { //creating a list of urls that will be populated with the urls of a given user List <URLViewModel> urls = null; int key = user.ID; //getting the data by invoking the web service using (var client = new HttpClient()) { client.BaseAddress = new Uri("https://localhost:5001"); //HTTP GET var responseTask = client.GetAsync($"URL?id={key}"); responseTask.Wait(); var result = responseTask.Result; if (result.IsSuccessStatusCode) { var readTask = result.Content.ReadAsAsync <List <URLViewModel> >(); readTask.Wait(); urls = readTask.Result; //if user is valid, redirect if (user != null) { URLViewModel testModel = new URLViewModel(); testModel.ID = user.ID; return(View(testModel)); } } else //web api sent error response { //log response status here.. URLViewModel userURL = new URLViewModel(); ModelState.AddModelError(string.Empty, "Server error. Please contact administrator."); } } return(View(new URLViewModel())); }
public IActionResult Long(string ShortenedURL) { URLViewModel OriginalUrl = null; using (var client = new HttpClient()) { client.BaseAddress = new Uri("https://localhost:5001"); //HTTP GET var responseTask = client.GetAsync($"Long?shortenedurl={ShortenedURL}"); responseTask.Wait(); var result = responseTask.Result; if (result.IsSuccessStatusCode) { var readTask = result.Content.ReadAsAsync <URLViewModel>(); readTask.Wait(); OriginalUrl = readTask.Result; //if url is valid if (OriginalUrl != null) { ViewBag.Original = OriginalUrl.LongURL; URLViewModel temp = new URLViewModel(); return(View(temp)); } } else //web api sent error response { //log response status here.. URLViewModel userURL = new URLViewModel(); ModelState.AddModelError(string.Empty, "Server error. Please contact administrator."); } } return(View(new URLViewModel())); }
public JsonResult DomainIsAvailable(URLViewModel urlModel) { if (ModelState.IsValid) { if (UtilityHelper.VerifyCaptcha(urlModel.RecaptchaResponse)) { return(Json(new { Success = false, Message = "La captcha est invalide" }, JsonRequestBehavior.AllowGet)); } var result = URLService.CheckDomainIsAvailable(urlModel.URL); if (result != null) { if (result.OperationSuccess) { return(Json(new { Success = true, Message = "Nom de domaine disponible" }, JsonRequestBehavior.AllowGet)); } else if (result.ErrorType.HasValue) { string message = string.Empty; switch (result.ErrorType.Value) { case DTO.Enum.ErrorType.EmptyRequest: message = "Veuillez saisir un nom de domaine."; break; case DTO.Enum.ErrorType.Exist: message = $"{urlModel.URL} est déjà enregistré."; break; case DTO.Enum.ErrorType.InvalidUrl: message = "Le nom de domaine n'est pas valide. Veuillez le saisir à nouveau."; break; case DTO.Enum.ErrorType.InternalServerError: message = "Un problème est survenu lors du service qui permet de valider l'url. Veuillez réessayer s’il vous plait."; break; } return(Json(new { Success = false, Message = message }, JsonRequestBehavior.AllowGet)); } } else { return(Json(new { Success = false, Message = "Un problème est survenu lors de la connexion. Veuillez réessayer s’il vous plait." }, JsonRequestBehavior.AllowGet)); } } return(Json(new { Success = false, Message = "Vos informations fournies sont invalides." }, JsonRequestBehavior.AllowGet)); }
public IActionResult Index(string LongURL) { URLViewModel OriginalUrl = null; URLViewModel newURL = new URLViewModel(); newURL.LongURL = LongURL; /*for the method of how to truncate the URLS, I thought about just taking off the slash that would * come after the .com and replacing it with a GUID, but though that would be too long and considered an alternative * route of simply generating a random number of at maximum 6 digits to be displayed after * the .com/net/etc*/ int cutOff; string beforeGuid; if (LongURL.IndexOf(".com") != -1) { cutOff = LongURL.IndexOf(".com"); beforeGuid = LongURL.Substring(0, cutOff + 4); } else if (LongURL.IndexOf(".gov") != -1) { cutOff = LongURL.IndexOf(".gov"); beforeGuid = LongURL.Substring(0, cutOff + 4); } else if (LongURL.IndexOf(".net") != -1) { cutOff = LongURL.IndexOf(".net"); beforeGuid = LongURL.Substring(0, cutOff + 4); } else if (LongURL.IndexOf(".co") != -1) { cutOff = LongURL.IndexOf(".co"); beforeGuid = LongURL.Substring(0, cutOff + 3); } else { cutOff = LongURL.IndexOf(".us"); beforeGuid = LongURL.Substring(0, cutOff + 3); } Random random = new Random(); int randomEnd = random.Next(0, 100000); string finalURL = beforeGuid + "/" + randomEnd; newURL.ShortenedURL = finalURL; //after the url had been shortened, then had to use the web service to write the data to the db using (var client = new HttpClient()) { client.BaseAddress = new Uri("https://localhost:5001"); //HTTP Post var toSend = newURL; var responseTask = client.PostAsync($"Shortener?longurl={newURL.LongURL}&shorturl={newURL.ShortenedURL}", null); responseTask.Wait(); var result = responseTask.Result; if (result.IsSuccessStatusCode) { var readTask = result.Content.ReadAsAsync <URLViewModel>(); readTask.Wait(); OriginalUrl = readTask.Result; //if user is valid, redirect if (OriginalUrl != null) { ViewBag.ShortenedURL = OriginalUrl.ShortenedURL; URLViewModel Temp = new URLViewModel(); Temp.ID = 1; return(View(Temp)); } } else //web api sent error response { //log response status here.. URLViewModel userURL = new URLViewModel(); ModelState.AddModelError(string.Empty, "Server error. Please contact administrator."); } } return(View(new URLViewModel())); }