Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

  • 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
like image 530
SivaRajini Avatar asked Mar 16 '13 07:03

SivaRajini


People also ask

Why do we need Web API in MVC?

Web API helps in enabling the development of HTTP services to reach out to client entities like browser, devices or tablets. ASP.NET Web API can be used with MVC for any type of application. A web API can help you develop ASP.NET application via AJAX.

What is the difference between RESTful API and Web API?

1) Web API vs REST API: ProtocolWeb API supports protocol for HTTP/s protocol and URL requests/responses headers that enable services to reach various clients through the web. On the other hand, all communication in the REST API is supported only through HTTP protocol.

Why Web API is better than MVC?

Web API can return data as JSON, XML, and other formats, but MVC only returns data as JSON using JSONResult. MVC does not support content negotiation or self-hosting, while Web API does. Even though Web API supports features of MVC, like routing and model binding, they are different, coming from System.

What is the difference between MVC and REST API?

MVC is about how the inner side of your app works. REST is about how your app "talks" with other apps. You can combine them. MVC is a design pattern for creating a separation of concerns and avoiding tightly coupled data, business, and presentation logic.


1 Answers

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.

like image 147
Erik Funkenbusch Avatar answered Oct 29 '22 20:10

Erik Funkenbusch