Recently Microsoft released ASP.NET MVC 4 Beta. The new Web API framework is included along with this release as part of the ASP.NET and Microsoft web platform family. The Web API framework is Microsoft’s latest solution to creating simple RESTful web services that can be consumed by a large range of clients. Unlike asmx and WCF services the Web API aims to be a simple RESTful way to create web APIs.
If you do not have Visual Studio 2010 get the free Visual Web Developer Express version here:
Next install MVC 4 (currently in Beta, supports a go-live license):
Creating a simple Web API:
Create a new project and select “ASP.NET MVC 4 Web Application”.
Next choose the “Internet Application” template. If you just want a Web API app then choose the "Web API" template.
In the Solution Explorer right-click the “Controllers” folder and select “Add” > “Controller...”
Add a simple class to your controller so that our new web api can return some data.
Replace your controller’s Get methods with code to return a list of books or a single book.
Start your project by pressing “F5” or choosing “Debug” > “Start Debugging”
You should see your web site’s homepage, but we want to see our new web api in action so type “/api/Books” after your site’s URL.
Our books have been returned to the browser in XML format. Ready to be consumed by virtually any type of client without having to deal with data contracts or complicated configuration. Depending on your client the Web API can return XML, JSON or form-urlencoded data. Media formatters can be written to support additional types of data formats.
Now add a “/4” to your URL and you should see a single book returned.
Web API is closely related to ASP.net MVC. Naming in particular determines which method gets called for a request. The main verbs used most frequently for CRUD (Create, Read, Update, Delete) are supported. Methods with names starting with “Get” are called on get requests, methods starting with Post for post requests, methods starting with Put handle put requests and methods starting with Delete handle delete requests.
While it looks similar to ASP.net MVC Web API can be used in many scenarios. Some other nice features of the Web API: