Last post Apr 12, 2017 01:16 PM by kaushalparik27
Apr 12, 2017 11:52 AM|obrienkev|LINK
I have a POST method in my controller. This method has a return type of void. Should I be using
IHttpActionResult as the return type? I am looking to find what is best practice.
Should my repository method also return a type other than void? It calls a stored proc that just does an insert into a table.
public void CreateUser([FromBody] UserDTO newUser)
public void CreateNewUser(string name, string email)
Apr 12, 2017 01:03 PM|mgebhard|LINK
A POST API should, at the very least, return a status code which indicates the result of the operation.
A create in general, IMHO, returns the new identity; otherwise the client can't uniquely identify the new record. This can lead to unexpected data issues. For example, how do you handle the newly created user if you need to do something else like add a
role? Or what if the insert failed due to a DB constraint like duplicate email or user name? This is more of a general design idea and has nothing to do with Web API.
Apr 12, 2017 01:16 PM|kaushalparik27|LINK
In old way, we used to pass HttpResponseMessage with appropriate HttpStatusCode to indicate a proper response. With WebAPI2 it is
changed enhanced to a new way to send response using IHttpActionResult. There are several benefits of IHttpActionResult that mentioned in
Microsoft ASP.Net Documentation:
Other advantages to consider are:
You can still use HttpResponseMessage. That capability will not go away. However, by creating IHttpActionResults that can be explicitly chained in your action method you can compose all kinds of different behaviour to generate your response.
Some useful links over this topic that you may would like to refer: