Reputation: 4005
I have confusion with WebAPIs.
Suppose I want a JSON result from a URL which I can use on client side to perform a particular operation.
I have Controller as below:
[HttpGet]
public string GetUsers()
{
List<Users> _u = new List<Users>();
_u.Add(new User() { Name = "Jon", Age = "22" });
_u.Add(new User() { Name = "Doe", Age = "24" });
_u.Add(new User() { Name = "Mike", Age = "20" });
return JsonConvert.SerializeObject(_u);
}
Above controller will return JSON data perfectly.
So what is the need to using WebAPI at all?
Upvotes: 0
Views: 26
Reputation: 58931
You are doing the serialization yourself which you don't have to do in a WebAPI method (just return the user list). You are also limiting your API to JSON whereas you can usually specify the content type when working with WebAPI. Also WebAPI provides you a lot of helper functions (for example returning NotFound() for a 404).
Upvotes: 1