title: httpclient post async returning method not allowed (405)
body: <p>Currently I'm having an issue with posting some data to web api.  I have two different project.  One project is a web application that is running web api 2.0.  The other project is a simple console application that creates a new http client, builds up a json object, and attempts to post it to the web api controller.  However, doing posts I keep getting 405 (Method not allowed...).  I've scoured the internet looking for solutions.  I've even gone as far as checking handlers in IIS Express and comparing that with what I have in web.config.  Still no luck.</p>
  
  <p>So here is what I have for my web api:</p>
  
  <pre><code>[RoutePrefix("api/media")]
  [Authorize]
  public class MultimediaController : ApiController
  {
      [HttpPost, Route("test")]
      public IHttpActionResult Store([FromBody] TestRequest request) {
          return Ok(request);
      }
  ...
  </code></pre>
  
  <p>Next, here is my Test model:</p>
  
  <pre><code>public class TestRequest {
      public string testProp1 { get; set; }
      public string testProp2 { get; set; }
  }
  </code></pre>
  
  <p>Then, here is a snip of code from my console app that attempts to do the POST:</p>
  
  <pre><code>        private const string testUrl = "/api/media/test/";
  
          private async Task&lt;bool&gt; RunPostTest()
          {
              var httpClient = CreateHttpClient();
              var content = JsonConvert.SerializeObject(new TestRequest {testProp1 = "hello", testProp2 = "world"});
              var jsonContent = new StringContent(content, Encoding.UTF32, "application/json");
  
              var response = await httpClient.PostAsync(testUrl, jsonContent);
  
              if (response.StatusCode != System.Net.HttpStatusCode.OK)
              {
                  Console.WriteLine($"Response code: {response.StatusCode}");
                  return false;
              }
  
              return true;
          }
  </code></pre>
  
  <p>Here is my web.config for handlers:</p>
  
  <pre><code>  &lt;system.webServer&gt;
      &lt;validation validateIntegratedModeConfiguration="false" /&gt;
      &lt;handlers&gt;
        &lt;remove name="ExtensionlessUrlHandler-Integrated-4.0" /&gt;
        &lt;remove name="OPTIONSVerbHandler" /&gt;
        &lt;remove name="TRACEVerbHandler" /&gt;
        &lt;add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" /&gt;
      &lt;/handlers&gt;
  </code></pre>
  
  <p>I checked iisexpress applicationhost.config.  Lots of handlers in there, but I'll just post the last few as those seem like the pertinent ones.  Also note that WebDAV is all commented out everywhere...</p>
  
  <pre><code>        ...
          &lt;add name="TRACEVerbHandler" path="*" verb="TRACE" modules="ProtocolSupportModule" requireAccess="None" /&gt;
          &lt;add name="OPTIONSVerbHandler" path="*" verb="OPTIONS" modules="ProtocolSupportModule" requireAccess="None" /&gt;
          &lt;add name="ExtensionlessUrl-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" /&gt;
          &lt;add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" /&gt;
          &lt;add name="ExtensionlessUrl-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" responseBufferLimit="0" /&gt;
          &lt;add name="StaticFile" path="*" verb="*" modules="StaticFileModule,DefaultDocumentModule,DirectoryListingModule" resourceType="Either" requireAccess="Read" /&gt;
      &lt;/handlers&gt;
  &lt;/system.webServer&gt;
  </code></pre>
  
  <p>This constantly returns an http 405.  I have other methods in here that perform GETs, and those hit the end points just fine.  </p>
  
  <p>Also worth noting I used an external app to perform a post, such as Postman, sending the raw json in the body.  And the endpoint was hit!  Seems like it has something to do with the set up of iisexpress, but I could be wrong.</p>
  
  <p>Anyone have any idea what could be the case?  Or refer me to some link that could uncover the problem?</p>
  
  <p>response object I'm getting back from debugging using immediate window:</p>
  
  <pre><code>response
  {StatusCode: 405, ReasonPhrase: 'Method Not Allowed', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
  {
    Pragma: no-cache
    X-SourceFiles: =?UTF-8?B?QzpcVXNlcnNcZ204NDBkXERvY3VtZW50c1xQcm9qZWN0c1xWaXN1YWwgU3R1ZGlvIFByb2plY3RzXEdpdFxNb2JpbGl0eSBDT0VcU0JWVFxTQlZUXFNCVlRcYXBpXG1lZGlhXHRlc3Rc?=
    Cache-Control: no-cache
    Date: Mon, 16 Jan 2017 17:39:32 GMT
    Server: Microsoft-IIS/10.0
    X-AspNet-Version: 4.0.30319
    X-Powered-By: ASP.NET
    Content-Length: 1218
    Allow: POST
    Content-Type: text/html
    Expires: -1
  }}
      Content: {System.Net.Http.StreamContent}
      Headers: {Pragma: no-cache
  X-SourceFiles: =?UTF-8?B?QzpcVXNlcnNcZ204NDBkXERvY3VtZW50c1xQcm9qZWN0c1xWaXN1YWwgU3R1ZGlvIFByb2plY3RzXEdpdFxNb2JpbGl0eSBDT0VcU0JWVFxTQlZUXFNCVlRcYXBpXG1lZGlhXHRlc3Rc?=
  Cache-Control: no-cache
  Date: Mon, 16 Jan 2017 17:39:32 GMT
  Server: Microsoft-IIS/10.0
  X-AspNet-Version: 4.0.30319
  X-Powered-By: ASP.NET
  }
      IsSuccessStatusCode: false
      ReasonPhrase: "Method Not Allowed"
      RequestMessage: {Method: POST, RequestUri: 'http://localhost:45106/api/media/test/', Version: 1.1, Content: System.Net.Http.StringContent, Headers:
  {
    Accept: application/json
    Content-Type: application/json; charset=utf-32
    Content-Length: 164
  }}
      StatusCode: MethodNotAllowed
      Version: {1.1}
  </code></pre>