title: How to Consume Web Api in xamarin CrossPlatform Application
body: <p>I have created web api that retrieves data from SQL database. I need to consume web api in xamrin for android and xamarin for iOS. as of now xamarin for android. I am not sure how to call GET and Post method based on button click event.</p>
  
  <p>Here is web api</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Net;
  using System.Net.Http;
  using System.Web.Http;
  
  namespace MvcAppDept.Controllers
  {
  public class DeptController : ApiController
  {
      // GET api/values
      public IEnumerable&lt;Dept&gt; Get()
      {
          AndroidAppDBEntities db = new AndroidAppDBEntities();
          var data = from dept in db.Depts orderby dept.no select dept;
          return data.ToList();
      }
  
      // GET api/values/5
      public Dept Get(int id)
      {
          AndroidAppDBEntities db = new AndroidAppDBEntities();
          var data = from dept in db.Depts where dept.no == id select dept;
          return data.SingleOrDefault();
      }
  
      // POST api/values
      public void Post(Dept obj)
      {
          AndroidAppDBEntities db = new AndroidAppDBEntities();
          db.Depts.Add(obj);
          db.SaveChanges();
      }
  
      // PUT api/values/5
      public void Put(int id,Dept obj)
      {
          AndroidAppDBEntities db = new AndroidAppDBEntities();
          var data = from dept in db.Depts where dept.no == id select dept;
          Dept old = data.SingleOrDefault();
          old.no = obj.no;
          old.name = obj.name;
          db.SaveChanges();
      }
  
      // DELETE api/values/5
      public void Delete(int id)
      {
          AndroidAppDBEntities db = new AndroidAppDBEntities();
          var data = from dept in db.Depts where dept.no == id select dept;
          Dept obj = data.SingleOrDefault();
          db.Depts.Remove(obj);
          db.SaveChanges();
  
      }
   }
  }
  
  
  Global.asax.cs
  
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.Http;
  using System.Web.Mvc;
  using System.Web.Optimization;
  using System.Web.Routing;
  
   namespace MvcAppDept
  {
  
  
  public class WebApiApplication : System.Web.HttpApplication
  {
      protected void Application_Start()
      {
          AreaRegistration.RegisterAllAreas();
  
          WebApiConfig.Register(GlobalConfiguration.Configuration);
          FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
          RouteConfig.RegisterRoutes(RouteTable.Routes);
          BundleConfig.RegisterBundles(BundleTable.Bundles);
          GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();
  
  
      }
     }
  </code></pre>
  
  <p>}</p>
  
  <p>MainActivity.cs</p>
  
  <pre><code>using System;
  using System.Net.Http;
  using System.Threading.Tasks;
  using Android.App;
  using Android.Content;
  using Android.Runtime;
  using Android.Views;
  using Android.Widget;
  using Android.OS;
  namespace DeptAndroidApp
  {
      [Activity(Label = "DeptAndroidApp", MainLauncher = true, Icon = "@drawable/icon")]
      public class MainActivity : Activity
      {
  
  
          protected override void OnCreate(Bundle bundle)
          {
              base.OnCreate(bundle);
              SetContentView(Resource.Layout.Main);
              Button GetButton = FindViewById&lt;Button&gt;(Resource.Id.GetButton);
              var txtValue1 = FindViewById&lt;EditText&gt;(Resource.Id.txtValue1);
              int id = Convert.ToInt32(txtValue1.Text);
              var No = FindViewById&lt;TextView&gt;(Resource.Id.No);
              var Name = FindViewById&lt;TextView&gt;(Resource.Id.Name);
              //GetButton.Click += GetDept(id); 
              GetButton.Click+=async(sender,args)=&gt;{
             //error  Cannot await 'DeptAndroidApp.dept                  
             Dept dept=await GetDept(id);
                  No.Text=dept.No;
                  Name.Text=dept.name}
          }
          //Error The return type ofasync method must be void,task orTask&lt;T&gt;
          public async Dept GetDept(int id)
          {
              using (var client = new HttpClient())
              {
                  client.BaseAddress = new Uri("http://****.***/");
                  client.DefaultRequestHeaders.Accept.Clear();
                  client.DefaultRequestHeaders.Accept.Add(new               System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
                  var result= await client.GetAsync(string.Format("/api/dept/{0}",id));
                 return JsonConvert.DeserializeObject&lt;Dept&gt;(await result.Content.ReadAsStringAsync());                }       
          }
  
      }
  }
  </code></pre>