Question

Je développe une application ASP.NET MVC 3. Je besoin de cette application faire usage d'une API Je dois aussi mettre en œuvre. L'API devrait à la fois être disponible à partir des actions du contrôleur ASP.NET MVC et Ajax. Maintenant, il est assez facile de faire une API en utilisant ASP.NET MVC, mais est-il possible d'utiliser ce produit des autres actions de site Web ASP.NET MVC? Je suppose que le Fonds de roulement est assez facile à utiliser car il est juste une référence de service.

D'autres utilisateurs de l'API peuvent être Windows Phone et iPhone.

Mise à jour:

Beaucoup ne voit que l'API comme un endroit où vous consommez des données, mais que dire de la partie où vous voulez exécuter des commandes ou faire des choses, comme client ajouter ou modifier foo?

Était-ce utile?

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

Autres conseils

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?

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top