title: 405 method not allowed error when making http post request from angular to web api
body: <p>I have an angular app with a .net core web-api as the back end.
  the get request from the client works as expected and data from the server is fetched.</p>
  <p>but when i try to post from the client to the server i get a 405 method not allowed error.</p>
  <p>i have implemented the CORS middle ware found at <a href="https://stackoverflow.com/a/45844400/6617849">this answer</a> :</p>
  <pre><code>    httpContext.Response.Headers.Add(&quot;Access-Control-Allow-Origin&quot;, &quot;http://localhost:4200&quot;);
      httpContext.Response.Headers.Add(&quot;Access-Control-Allow-Credentials&quot;, &quot;true&quot;);
      httpContext.Response.Headers.Add(&quot;Access-Control-Allow-Headers&quot;, &quot;Content-Type, X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Date, X-Api-Version, X-File-Name&quot;);
      httpContext.Response.Headers.Add(&quot;Access-Control-Allow-Methods&quot;, &quot;POST,GET,PUT,PATCH,DELETE,OPTIONS&quot;);
  </code></pre>
  <p><strong>web-api controller</strong></p>
  <pre><code>[Route(&quot;api/[controller]&quot;)]
  [ApiController]
  public class PostsController : ControllerBase
  {
      [HttpGet]
      public ActionResult&lt;List&lt;Post&gt;&gt; Get()
      {
          return _repository.Get().ToList();
      }
  
      [HttpPost]
      public IActionResult Post([FromBody] Post post)
      {
          if (post == null)
          {
              return BadRequest();
          }
          _repository.Add(post);
          _repository.Save();
          return CreatedAtRoute(&quot;GetPostById&quot;, new { ID = post.Id }, post);
      }
  }
  </code></pre>
  <p><strong>Post model on server</strong></p>
  <pre><code>   public class Post 
      {
          [Key]
          public int Id { get; set; }
          public string Title { get; set; }
          public string Content { get; set; }
      }
  </code></pre>
  <p><strong>app.component.ts</strong></p>
  <pre><code>  apiUrl = 'http://localhost:33633/api/posts';
    constructor(private http: HttpClient) {}
  
  onCreatePost(postData: { title: string; content: string }) {
  
  this.http.post(this.apiUrl, postData).subscribe(responseData =&gt;
  {
    console.log(responseData);
  })
  }
    onFetchPosts() {
      this.http.get(this.apiUrl).subscribe(responseData =&gt;
      {
        console.log(responseData);
      })
    }
  </code></pre>
  <p><strong>console error when trying to post to server:</strong>
  <a href="https://i.stack.imgur.com/iwFEe.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/iwFEe.jpg" alt="enter image description here" /></a></p>
  <p>what could be the reason for this error?</p>
  <p><strong>---Edit - found a solution---</strong>
  in <a href="https://stackoverflow.com/a/21458845/6617849">this answer</a></p>
  <p>i added a base controller with the method</p>
  <pre><code>public HttpResponseMessage Options()
  {
    return new HttpResponseMessage { StatusCode = HttpStatusCode.OK };
  }
  </code></pre>
  <p>and now it works.</p>