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.