SivaRajini
SivaRajini

Reputation: 7375

Why do we need web API in MVC? What's the drawback in restful api in mvc?

I am new to ASP.NET Web API. Can anyone please tell me

Upvotes: 8

Views: 10608

Answers (3)

Darrel Miller
Darrel Miller

Reputation: 142094

MVC is optimized to serve information to a web browser client. If your client is something else, Web API will make your life easier over the long term.

Web API is a from the ground up re-write of the web stack. At the core it is much cleaner and more flexible than the 12 year old infrastructure that MVC is built on top of. Web API does not yet have the same level of tooling, add-ons, plugins as MVC, but that will come.

Upvotes: 1

Erik Funkenbusch
Erik Funkenbusch

Reputation: 93444

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.

Upvotes: 12

Tom Stickel
Tom Stickel

Reputation: 20411

WCF team merged at Microsoft with MVC team. WCF is not going away, but for simple RESTFUL service call, the MVC Controller were a match made in heaven, and the modification to it allowed for a very easy Web API.

While many of us feel WCF is relatively easy, there are many who fear it and/or don't have/take time to learn it, thus they a. still use ASMX, b. still never adopted services, or NOW with Web API, are c. Very excited that they can very easily get up and running with restful web services.

So really it is a matter of comfort level, adaptation, ability to change and the Web API does have its place. It cannot replace WCF as WCF has advanced configurations with all the bindings and ability to do SOAP and not just REST, which many applications still NEED to have SOAP protocol.

Upvotes: 1

Related Questions