Deepika Chaudhary

Deepika Chaudhary

  • 1.6k
  • 136
  • 97.8k

Using web api how to work with controller

Jun 22 2019 12:12 AM
I am using following code. let me know any other way
  1. public async Task<ActionResult> Index()  
  2. {  
  3. string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;  
  4. string qName = userName.Replace("AMAT\\", "");  
  5. string apiUrl = "http://dca-dev-577/deepika.ldap/api/AD/seachADUsers?searchText="+qName;  
  6.   
  7. using (HttpClient client = new HttpClient())  
  8. {  
  9. client.BaseAddress = new Uri(apiUrl);  
  10. client.DefaultRequestHeaders.Accept.Clear();  
  11. client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));  
  12.   
  13. HttpResponseMessage response = await client.GetAsync(apiUrl);  
  14. if (response.IsSuccessStatusCode)  
  15. {  
  16. var data = await response.Content.ReadAsStringAsync();  
  17. var table = Newtonsoft.Json.JsonConvert.DeserializeObject<System.Data.DataTable>(data);  
  18.   
  19. JArray rss = JArray.Parse(data);  
  20. //JObject rss = JObject.Parse(data);  
  21. string FName = rss[0]["FirstName"].ToString();  
  22. string LName = rss[0]["LastName"].ToString();  
  23. ViewBag.message = "First Name: " + FName + " Last Name: " + LName;  
  24. }  
  25. }  
  26. return View();  
  27. }  

Answers (2)