Reputation: 29
We are developing a REST API and we're allowing all four of the standard verbs. In the case of an POST/PUT what is better in best practice c# rest api.
this is my Model
public class UserModel
{
public Int64 ID { get; set; }
[Display(Name ="First Name")]
public string FirstName { get; set; }
[Display(Name="Last Name")]
public string LastName { get; set; }
public string Address { get; set; }
[Display(Name="User Name")]
public string UserName { get; set; }
public string Email { get; set; }
public string Password { get; set; }
[Display(Name ="Added Date")]
public DateTime AddedDate { get; set; }
}
Exemple 1
[HttpPost]
public ActionResult CreateEditUser(UserModel model)
{
if (model.ID == 0)
{
User userEntity = new User
{
//....
}
}
}
Exemple 2
[HttpPost]
public ActionResult CreateEditUser(int id,UserModel model)
{
if (id == 0)
{
User userEntity = new User
{
//.....
}
}
}
what is the better Exemple 1 Or Exemple 2
Upvotes: 0
Views: 1404
Reputation: 516
According the REST guidelines (https://github.com/microsoft/api-guidelines/blob/vNext/Guidelines.md)
PUT: Replace an object, or create a named object, when applicable
POST: Create a new object based on the data provided, or submit a command
PATCH: Apply a partial update to an object
In your case is it better to split the endpoints into a POST and PUT.
[HttpPost]
public ActionResult CreateUser(UserModel model)
{
userService.Create(model);
return...
}
[HttpPut]
public ActionResult EditUser(UserModel model)
{
userService.Update(model);
return...
}
Upvotes: 1