public async Task <ActionResult> VisitShortenUrl(URLShortenViewModel reqInfo) { try { VisitedUserData vistedUser = new VisitedUserData(); IpInfo visitedIPData = new IpInfo(); // Private details vistedUser.HostMachineName = Dns.GetHostName(); vistedUser.MachinePrivateIP = Dns.GetHostByName(vistedUser.HostMachineName).AddressList[0].ToString(); // Public details visitedIPData = GetUserDetailByIp(GetPublicIP()); vistedUser.MachinePublicIP = visitedIPData.Ip; vistedUser.LongURL = reqInfo.longURL; vistedUser.ShortenURL = reqInfo.shortenURL; vistedUser.VisitedCountry = visitedIPData.Country; vistedUser.VisitedRegion = visitedIPData.Region; vistedUser.VisitedCity = visitedIPData.City; vistedUser.ServiceProviderName = visitedIPData.Hostname; // Save Details to DB return(Redirect(reqInfo.shortenURL)); } catch (Exception ex) { throw ex; } }
public ActionResult GenerateShortenUrl() { try { URLShortenViewModel urlRquestInfo = new URLShortenViewModel(); urlRquestInfo.longURL = null; urlRquestInfo.shortenURL = null; return(View(urlRquestInfo)); } catch (Exception ex) { throw ex; } }
public async Task <ActionResult> GenerateShortenUrl(URLShortenViewModel reqInfo) { URLShortenViewModel urlRquestInfo = new URLShortenViewModel(); if (ModelState.IsValid) { try { using (var client = new HttpClient()) { //Passing service base url client.BaseAddress = new Uri(Baseurl); client.DefaultRequestHeaders.Clear(); //Define request data format client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); //Sending request to find web api REST service resource GetAllEmployees using HttpClient HttpResponseMessage Res = await client.GetAsync("api/URLGenerate?longUrl=" + reqInfo.longURL); //Checking the response is successful or not which is sent using HttpClient if (Res.IsSuccessStatusCode) { //Storing the response details recieved from web api var URLShortenResponse = Res.Content.ReadAsStringAsync().Result; //Deserializing the response recieved from web api and storing into the Employee list urlRquestInfo = JsonConvert.DeserializeObject <URLShortenViewModel>(URLShortenResponse); } //returning the employee list to view return(View(urlRquestInfo)); } } catch (Exception ex) { throw ex; } } return(View(reqInfo)); }