Why do we need web API in MVC? What's the drawback in restful api in mvc?
asp.net-mvc-4, asp.net-web-api
Solution
WebAPI is based on MVC, but has some subtle differences. You need to understand that WebAPI is a separate thing from MVC, and does not require MVC. You can install WebAPI separately, and you can uninstall it from the default MVC templates.
It's true, MS could have built WebAPI directly into the MVC Controllers, but they chose to keep API Controllers separate from MVC Controllers because they really are different ways of dealing with requests and responses.
Examples of things you can do in WebAPI that you can't (or at least not as easily) in MVC include:
- Content Negotiation
- This allows the calling client to choose the format that data will be returned in, such as XML or JSON.
- OData support
- This allows the caller to "filter" results on the server without the service method having to specifically support it. For instance, if you want to sort the results by first name, then this can be done simply by specifying OData query parameters
WebAPI provides a lot of power for dealing with data result sets. MVC does not provide that kind of functionality.
You would tend to use WebAPI for things like Ajax requests, or web service based requests that do not require the complexity of WCF.
RESTful API's are not specific to MVC or WebAPI. They're simply a philosophy for how you design your HTTP requests in a service. There's a lot to it really, but I won't go into it.
Problem
I am new to ASP.NET Web API. Can anyone please tell me - Why we need Web API? - How it differs from rest full api from MVC ? - When to use MVC4 web api ? - What is restful api in MVC