C # Web API 2 - AJAX Request for Method PUT Returning 405 (Method Not Allowed)

0

Good morning! I'm building a system using C # as back and React on the front, and I'm having a problem with two controllers (supposedly), but the error seems to be the same, every time I make an AJAX request with method of type PUT , I get the error

  

PUT link 405 (Method Not Allowed)

Followed by

  

{Message: "The requested resource does not support the 'PUT' method."}

I read about CORS, I made the recommended adjustments on various topics, but the error remains. Here is the code:

$.ajax({
  url: URL,//A URL é montada acima, e para teste seu valor é http://localhost:49232/api/Products/1025
  contentType: 'application/json',
  dataType: 'JSON',
  data: { "product": json}, //Aqui passo o objeto montado
  type: 'PUT',
  beforeSend: function () {

  },
  success: function (response) {
    // do something
  }.bind(this),
  error: function (response) {
    // do something else
  }

});

Action C #:

[ResponseType(typeof(void))]
    public async Task<IHttpActionResult> PutProduct(int id, Product product)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        if (id != product.IdProduct)
        {
            return BadRequest();
        }

        db.Entry(product).State = EntityState.Modified;

        try
        {
            await db.SaveChangesAsync();
        }
        catch (DbUpdateConcurrencyException)
        {
            if (!ProductExists(id))
            {
                return NotFound();
            }
            else
            {
                throw;
            }
        }

        return StatusCode(HttpStatusCode.NoContent);
    }

If you can help me, thank you.

    
asked by anonymous 26.11.2018 / 13:51

1 answer

3

Ricardo, can your URL be solved? I did not do a table test here but your example uses the URL

  

link

And your method is waiting for two arguments (an integer and an object) and is named PutProduct and not Products.

    
26.11.2018 / 14:11