Question

I'm developing an ASP.NET MVC 3 application. I need this application to make use of an API I also need to implement. The API should both be available from ASP.NET MVC controller actions and Ajax. Now it is quite easy to make an API using ASP.NET MVC, but is it possible to use this from other ASP.NET MVC website actions? I guess the WCF is quite easy to use as it is just a service reference.

Other users of the API could be Windows Phone and iPhone.

Update:

Many only sees the API as a place where you consume data, but what about the part where you want to execute commands or do stuff, like add customer or change foo?

Was it helpful?

Solution

You may want to check our new WCF web API that was announced at PDC. We recently released a big upgrade. WCF Web API is designed specifically for allowing you to expose APIs to a range of clients in a pure HTTP manner. It is lightweight, offers a nice configuration story (no configuration files) and also is much easier to test.

You can download the bits at wcf.codeplex.com. It includes various samples, and there is also a set of NuGet packs for getting you started. Search for webapi.all on NuGet.

OTHER TIPS

The way I like to do this is with RESTful controller actions. You can return JSON and use your calls with JavaScript on your own site. Other websites would almost certainly understand JSON and so they'd be able to consume your API pretty easily. This is much easier to write and test than a heavy WCF layer.

Check out this question for some example REST frameworks for MVC:

ASP.NET MVC REST frameworks

One of the newer ways of accomplishing data feeds is using OData. Scott Hanselman has a great introduction to it in Creating an OData API for StackOverflow including XML and JSON in 30 minutes.

It allows you to even throw LINQ queries into your URLs to retrieve exactly the data you need.

WCF JSON binding was really terrible last time I used it. WCF also comes with all sorts of crazy rules about streams and how you have to use [MessageBody] attributes for everything.

It was a real PITA to me.


I knew I've answered something like this before:

What is the best way to implement a RESTful architecture in .NET today?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top