title: ASP.NET WebApi : (405) Method Not Allowed
body: <p>I have a Web Api controller. It behaves very strange. When I use PostMan I can access the POST method on the Web Api but when I use HttpWebRequest from .net it returns (405) Method Not Allowed.
  I put the Web Api code here:</p>
  
  <pre><code>using Newtonsoft.Json;
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Net;
  using System.Net.Http;
  using System.Web;
  using System.Web.Http;
  
  namespace MyProject.Controllers
  {
      public class TestController : ApiController
      {
          [HttpPost]
          public int buy(OrderResult value)
          {
              try
              {
                  if (value.InvoiceDate != null &amp;&amp; value.Result == 1)
                  {
                      return 0;
                  }
              }
              catch{}
  
              return -1;
          }
      }
  
      public class OrderResult
      {
          public int Result { get; set; }
          public long InvoiceNumber { get; set; }
          public string InvoiceDate { get; set; }
          public long TimeStamp { get; set; }
      }
  }
  </code></pre>
  
  <p>Here is my WebApiConfig.cs:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web.Http;
  
  namespace MyProject
  {
      public static class WebApiConfig
      {
          public static void Register(HttpConfiguration config)
          {
  
              config.Routes.MapHttpRoute(
                  name: "DefaultApi",
                  routeTemplate: "api/{controller}/{action}/{id}",
                  defaults: new { action = RouteParameter.Optional, id = RouteParameter.Optional }
              );
  
          }
      }
  }
  </code></pre>
  
  <p>This is how I send the POST request from another .NET project:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.IO;
  using System.Linq;
  using System.Net;
  using System.Text;
  using System.Web;
  
  namespace MyProject.Controllers
  {
      public static class WebReq
      {
          public static string PostRequest(string Url, string postParameters)
          {
              try
              {
                  HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(Url);
                  myReq.Method = "POST";
                  myReq.Timeout = 30000;
                  myReq.Proxy = null;
  
                  byte[] postData = Encoding.UTF8.GetBytes(postParameters);
                  myReq.ContentLength = postData.Length;
                  myReq.ContentType = "application/x-www-form-urlencoded";
                  using (Stream requestWrite = myReq.GetRequestStream())
                  {
                      requestWrite.Write(postData, 0, postData.Length);
                      requestWrite.Close();
                      using (HttpWebResponse webResponse = (HttpWebResponse)myReq.GetResponse())
                      {
                          if (webResponse.StatusCode == HttpStatusCode.OK)
                          {
                              using (Stream str = webResponse.GetResponseStream())
                              {
                                  using (StreamReader sr = new StreamReader(str))
                                  {
                                      return sr.ReadToEnd();
                                  }
                              }
                          }
                          return null;
                      }
                  }
              }
              catch (Exception e)
              {
                  var message = e.Message;
                  return null;
              }
          }
  
      }
  }
  </code></pre>
  
  <p>I already added the following code in my web.config:</p>
  
  <pre><code>&lt;modules runAllManagedModulesForAllRequests="true"&gt;
    &lt;remove name="WebDAVModule" /&gt;
  &lt;/modules&gt;
  </code></pre>
  
  <p>It is strange because I can send POST request successfully from PostMan. The PostMan send this code to the API.</p>
  
  <pre><code>POST /api/Test/buy HTTP/1.1
  Host: domain.com
  Cache-Control: no-cache
  Postman-Token: 9220a4e6-e707-5c5f-ea61-55171a5dd95f
  Content-Type: application/x-www-form-urlencoded
  
  InvoiceDate=28012016&amp;Result=1
  </code></pre>
  
  <p>I will appreciate any suggestion to solve the problem.</p>