I'm starting to develop a REST API using ASP.NET WebAPI2.
In my controller
, I'm using the PATCH
method to apply partial changes to a model
.
I have a method that looks 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 Nome
property of the model.
At the time of the request, the endpoint is
api/Clientes/1?novoNome=JoaquimAlbertoSilva
Following this logic, I would need to create three more methods, in order to give the client the option to edit only these 4 properties of the Client model.
My questions are:
-
Is this approach right? Should I just do one method?
-
Would it be better for me to define a route for each type of change? Something like:
api/Clientes/1/EditarNome/{novoNome}
api/Clientes/1/EditarApelido/{novoApelido}