title: Access to web API from a console application
body: <p>In the process of learning about web API, I have created (or actually Visual Studio has) a simple controller. I have also created a WPF program that can read from web API's GET() part, but I can't access the others.</p>
  
  <p>I have tried a lot and found many blogs and pages that say: "Just do like this..." but nothing works. What am I doing wrong?</p>
  
  <p>MVC part:</p>
  
  <pre><code>namespace MVCWebservice.Controllers
  {
      public class LaLaController : ApiController
      {
          // GET: api/LaLa
          public string Get()
          {
              return "Hello from API";
          }
  
          // GET: api/LaLa/5
          public string Get(int id)
          {
              return "value";
          }
  
          // POST: api/LaLa
          public void Post([FromBody]string value)
          {
              var a = value;
          }
  
          // PUT: api/LaLa/5
          public void Put(int id, [FromBody]string value)
          {
              var b = value;
              int c = id;
          }
  
          // DELETE: api/LaLa/5
          public void Delete(int id)
          {
              int c = id;
          }
      }
  }
  </code></pre>
  
  <p>And a method from my Console application that actually works:</p>
  
  <pre><code>private static async Task ReadFromWebApi()
  {
      System.Net.Http.HttpClient client = new System.Net.Http.HttpClient();
      client.BaseAddress = new Uri("http://localhost:26176/");
      client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
      var resp2 = await client.GetAsync("api/LaLa/");
      resp2.EnsureSuccessStatusCode();
      var aaa = resp2.Content;
      string result = await aaa.ReadAsStringAsync();
      Console.WriteLine(result);
  }
  </code></pre>
  
  <p>A method that just stops:</p>
  
  <p>If I remove the <code>EnsureSuccessStatusCode</code> I'll get the following back: </p>
  
  <blockquote>
    <p>ss = "{\"Message\":\"The requested resource does not support http
    method 'PUT'.\"}"</p>
  </blockquote>
  
  <pre><code>private static async Task SendToWebApi()
  {
      System.Net.Http.HttpClient client = new System.Net.Http.HttpClient();
      client.BaseAddress = new Uri("http://localhost:26176/");
      client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
      Console.WriteLine("-1-");
      var resp2 = client.PutAsync("api/LaLa/", new System.Net.Http.StringContent("Hey", Encoding.UTF8, "application/json")).Result;
      Console.WriteLine("-2-");
      resp2.EnsureSuccessStatusCode();
      var ss = await resp2.Content.ReadAsStringAsync();
  }
  </code></pre>
  
  <p>How would I write my access to the other methods?</p>