7
I am starting to develop a REST API using ASP.NET Webapi2.
In my controller
, I’m using the method PATCH
to apply partial changes to a model
.
I have a method that is this way
[HttpPatch]
[ResponseType(typeof(void))]
public IHttpActionResult EditarNome(int id, string novoNome)
{
var cliente = _db.Clientes.Find(id);
if (cliente == null)
{
return NotFound();
}
cliente.Nome = novoNome;
_db.Entry(cliente).State = EntityState.Modified;
_db.SaveChanges();
return StatusCode(HttpStatusCode.NoContent);
}
This method, takes a new name as parameter and updates the property Nome
of the model.
When the request is made the endpoint is
api/Clientes/1?novoNome=JoaquimAlbertoSilva
Following this logic, I would need to create three more methods, to be able to give the client the option to edit only these 4 model properties Client.
My doubts are:
Is this approach correct? Should I do just one method?
Would it be better if I set a route for each type of change? Something like:
api/Clientes/1/EditarNome/{novoNome}
api/Clientes/1/EditarApelido/{novoApelido}
Much show the
Delta<T>
. +1.– Leonel Sanches da Silva
Incredible. I intend to test the
Delta<T>
yet. The problem is that I can’t open the client to edit the entire model. Using theDelta<T>
the customer can send the whole object and make it be completely updated.– Jéf Bueno
After thinking a little bit about it, I think this is actually a much more elegant solution. Thanks for the contribution!
– Jéf Bueno