0

I have a Web Api project with a controller that has methods for GET, DELETE, POST, and PUT.

When I try to do a POST or PUT to this controller I always get a 405 Method Not Allowed error. The data being sent over looks valid, it's just an object with six simple properties. I put a breakpoint in my method and as expected in this case, it doesn't get hit. I registered a DelegatingHandler (mentioned at Web Api - Catch 405 Method Not Allowed) to inspect the incoming request and outgoing response and I can tell that my request is being processed by the Api (meaning the problem is not with the client). I also used Fiddler to inspect the request/response and the response headers say under Security, Allow: DELETE, GET.

This clearly tells me that PUT and POST are not allowed, for whatever reason, even though I have methods decorated with the [HttpPost] and [HttpPut] attributes and have the routing configured correctly, as far as I can tell. I am using default routing but also have methods which use attribute routing.

This sounds like there may be some kind of security issue, however, I'm able to do POST and PUT in my other controllers and I don't see any differences which I believe would be the cause of the problem.

Here's a snippet of my code:

public class PricesController : ApiController
{
    // DELETE: api/Prices/5
    [HttpDelete]
    [ResponseType(typeof(Price))]
    [Route("api/Prices/{id:int}")]
    public async Task<IHttpActionResult> DeletePrice(int id)
    {
      // code omitted
    }

    // GET: api/Prices/5
    [HttpGet]
    [ResponseType(typeof(Price))]
    [Route("api/Prices/{id:int}")]
    public async Task<IHttpActionResult> GetPrice(int id)
    {
      // code omitted
    }

    // GET: api/Prices
    [HttpGet]
    [Route("api/Prices")]
    public IQueryable<Price> GetPrices()
    {
      // code omitted
    }

    // POST: api/Prices
    [HttpPost]
    [ResponseType(typeof(Price))]
    [Route("api/Prices", Name = "Prices")]
    public async Task<IHttpActionResult> PostPrice(Price price)
    {
      // code omitted
    }

    // PUT: api/Prices/5
    [HttpPut]
    [ResponseType(typeof(void))]
    [Route("api/Prices/{id:int}")]
    public async Task<IHttpActionResult> PutPrice(int id, Price price)
    {
      // code omitted
    }
}

Any help would be appreciated. I've spent all day trying to figure this out.

Community
  • 1
  • 1
DesertFoxAZ
  • 439
  • 1
  • 4
  • 14
  • A few things to check. make sure that attribute routing is enable before convention based routes in WebApiConfig. check web.config to make sure the verb is allowed for paths in the handler. also show how you call the actions. what URL do you use – Nkosi Apr 14 '17 at 22:49
  • Attribute routing is enabled, I've been using it from some time. There are 56 other controllers in this app and most of them support GET, DELETE, POST and PUT operations. The client is using Angular 1.x with resources, again, this is using a similar pattern used throughout the application where I am not having this problem. – DesertFoxAZ Apr 14 '17 at 23:00
  • Questions seeking debugging help ("**why isn't this code working?**") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it **in the question itself**. Questions without **a clear problem statement** are not useful to other readers. See: How to create a [mcve]. – Nkosi Apr 14 '17 at 23:02

1 Answers1

0

It sounds like it's not binding correctly.

Can you try decorating Price with [FromBody] before it in your actions?

PostPrice([FromBody] Price price)
Eilimint
  • 307
  • 1
  • 3
  • 11
  • I tried that and it makes no difference. I haven't needed to use that attribute anywhere else in the application. I have plenty of other controllers that are fairly similar that don't have this problem. – DesertFoxAZ Apr 14 '17 at 22:47
  • Is the content type of the request set as application/json? – Eilimint Apr 14 '17 at 22:50
  • Yes, the client app is Angular 1.x and is rather large. Up to this point everything was working but this particular functionality was missing so I am adding it. I wrote most of the application so I am very familiar with it. – DesertFoxAZ Apr 14 '17 at 22:57
  • Since you mentioned it was a large app, are you sure that route /api/prices is not already being used? – Eilimint Apr 14 '17 at 23:05
  • @Noel if there were conflicting routes it would give a different error. – Nkosi Apr 14 '17 at 23:14