locked
Calling Webapi 2 service from MVC 5 controller RRS feed

  • Question

  • User895212817 posted

    I want to call a webapi 2 service from MVC 5 controller, can anybody please help me out and give me some easy to understand examples.

    Thanks.

    ASAD

    Thursday, May 7, 2015 9:25 PM

Answers

  • User-782957977 posted

    You can call with following code

    Web Api Controller

     public class ValuesController : ApiController
     {
          
            // GET api/values/5
            public string Get(int id)
            {
                return "value";
            }
    
      }

    Code to call Web Api from MVC

     using (var client = new HttpClient())
                 {
                     client.BaseAddress = new Uri("http://localhost:58745");
                     client.DefaultRequestHeaders.Accept.Clear();
                     client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                     var response = client.GetAsync("api/values/10").Result;
                     if (response.IsSuccessStatusCode)
                     {
                         string responseString = response.Content.ReadAsStringAsync().Result;
                     }
                 }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 7, 2015 11:37 PM

All replies

  • User-782957977 posted

    You can call with following code

    Web Api Controller

     public class ValuesController : ApiController
     {
          
            // GET api/values/5
            public string Get(int id)
            {
                return "value";
            }
    
      }

    Code to call Web Api from MVC

     using (var client = new HttpClient())
                 {
                     client.BaseAddress = new Uri("http://localhost:58745");
                     client.DefaultRequestHeaders.Accept.Clear();
                     client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                     var response = client.GetAsync("api/values/10").Result;
                     if (response.IsSuccessStatusCode)
                     {
                         string responseString = response.Content.ReadAsStringAsync().Result;
                     }
                 }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 7, 2015 11:37 PM
  • User895212817 posted

    Hi Santoshje, Thanks for your reply mate, i just have query regarding async and synchronous call, this is i think async call, can you please tell me if i want to do synchronous call what should it be.

    Best regards,

    ASAD

    Friday, May 8, 2015 1:03 AM
  • User-782957977 posted

    Please use following code

    using(var client = new WebClient())
                {
                    client.Headers.Add("content-type", "application/json");//
                    string response = client.DownloadString("http://localhost:58745/api/values/10");
                }

    Friday, May 8, 2015 7:14 PM
  • User895212817 posted

    Thanks very much Sathoshje, much appreciated.

    Sunday, May 10, 2015 11:55 PM