I know what a Web API is.
I've written API's in multiple languages (including in MVC3).
I'm also well practiced in ASP.Net.
I just discovered that MVC4 has "Web API" and without going through the video examples I can't find a good explanation of what exactly it IS.
From my past experience, Microsoft technologies (especially ASP.Net) have a tendency to take a simple concept and wrap it in a bunch of useless overhead that is meant to make everything "easier".
Can someone please explain to me what Web API in MVC4 is exactly? Why do I need it? Why can't I just write my own API?
5 Answers
ASP.NET Web API is a "non-opinionated" framework to build HTTP Service regardless of REST or RPC. It is Microsoft's best implementation of RFC 2616 (HTTP Spec).
Certainly you can build your own API but ASP.NET Web API:
- Built based on the Russian Doll model which allows for lego-like modules to be added to the HTTP pipeline
- Makes HTTP first class citizen so all common headers are strongly typed (not just name value) and helps with parsing them
- Allows for both ASP.NET (IIS) hosting or self-hosting
- supports content negotiation, media types, ...
- Is Async from top to bottom
- Uses a similar approach for clients with HttpClient
The new Web API from Microsoft is, as its name suggests all about writing APIs that are web accessible, with a bent on REST.
Rick Strahl recently blogged about it and has a good analysis on when to use MVC vs Web API.
Things like built in content negotiation, several built in formats and more.
In short: ASP.NET Web API is making REST a first class citizen in .NET, which WCF was not really able to embrace. As you may know, ASP.NET Web API is a framework for building web APIs
on top of the .NET Framework.
One of the biggest frustration we had as web developers was to face with the endless configurations with WCF. I mean, WCF is great but hasn’t been embraced the REST since the WCF Web API framework . There was something called REST Starter Kit but it ended up dead.
However, ASP.NET Web API is a next generation framework that makes it easy to build HTTP services that reach a broad range of clients, such as browsers and mobile devices. It is an ideal platform for building RESTful applications on the .NET Framework. For more information and great blog posts, visit ASP.NET Web API bloggers page
-
1
-
Well, it is a lightweight replacement of it with important features to ease the configuration and streamline app development.– YusubovJan 4, 2013 at 18:36
-
@Yusobov: This is not quite true - There are also the WCF Data Services which are quite good at building Odata-compatible REST apis. I've been using it in a production environment and it really works well once it's set up. The only problem with that is that is much harder to set up than an ASP.NET Web API. Feb 7, 2014 at 15:21
ASP.NET Web API is a framework that makes it easy to build HTTP services that reach a broad range of clients, including browsers and mobile devices.
ASP.NET Web API is an ideal platform for building RESTful applications on the .NET Framework.
EDIT:
Specifically, ASP.NET Web API includes:
- A streamlined ASP.NET pipeline with reduced overhead if you choose to host your service in IIS.
- HttpClient which makes it easier for a client to hook into the Web API.
-
I can read those things myself. My question is what's the point? What is so different in using Web API and using regular MVC but having JsonResults instead of ViewResults? Aug 12, 2012 at 19:33
-
1. Before MVC 4, you could return a JSONResult without any issue. MVC 4 simply retains that capability, includes some other helper classes to make it easier, and streamlines the ASP.NET pipeline to reduce the overhead when you know that you won't need to return ViewResults.– Jim G.Aug 12, 2012 at 19:43
-
1Also, because it's a effectively an update for WCF Web API, you aren't wed to IIS. You can self-host an ASP.NET Web API service if you prefer. wcf.codeplex.com/…– Jim G.Aug 12, 2012 at 20:00
Today, a web-based application is not enough to reach it's customers. People are very smart, they are using iphone, mobile, tablets etc. devices in its daily life. These devices also have a lot of apps for making the life easy. Actually, we are moving from the web towards apps world. So, if you like to expose your service data to the browsers and as well as all these modern devices apps in fast and simple way, you should have an API which is compatible with browsers and all these devices.
-
this doesn't seem to add anything substantial over points made and explained in prior 4 answers– gnatJul 17, 2015 at 6:56
Why can't I just write my own API?
With just regular ASP.NET MVC? You can. ASP.NET MVC Web API just gives you extra goodies to make it easier. Please see below.