TechHui

Hawaiʻi's Technology and New Media Community

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.

Getting Started:

If you do not have Visual Studio 2010 get the free Visual Web Developer Express version here:
http://www.asp.net/get-started

Next install MVC 4 (currently in Beta, supports a go-live license):
http://www.asp.net/mvc/mvc4

 

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...”




Name your new controller and select “API controller with empty read/write actions” for the Template.



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:

  • It can be used in a WebForms or ASP.NET MVC app side by side with other web site files and resources.
  • Like WCF it can be self-hosted within another application, for example a console app, and does not need to run under IIS.
  • Supports OData URL queries, Model Binding, Validation, Authentication and has full .net framework support behind it.
I hope this post showed how simple and easy it is to create Web APIs.  It's just one of the many pieces of .net technologies being released in the months ahead.

Views: 538

Tags: API, ASP.NET, MVC, Web

Comment

You need to be a member of TechHui to add comments!

Join TechHui

Sponsors

web design, web development, localization

© 2014   Created by Daniel Leuck.

Badges  |  Report an Issue  |  Terms of Service