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.
我正在开发一个ASP.NET MVC 3应用程序。我需要这个应用程序来使用我还需要实现的API。 API应该都可以从ASP.NET MVC控制器操作和Ajax中获得。现在使用ASP.NET MVC制作API非常容易,但是可以从其他ASP.NET MVC网站操作中使用它吗?我想WCF很容易使用,因为它只是一个服务参考。
Other users of the API could be Windows Phone and iPhone.
API的其他用户可以是Windows Phone和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?
许多人只将API视为您使用数据的地方,但是您想要执行命令或执行操作的部分,例如添加客户或更改foo?
4 个解决方案
#1
3
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.
您可能想要检查我们在PDC上宣布的新WCF Web API。我们最近发布了一个重大升级。 WCF Web API专门用于允许您以纯HTTP方式向一系列客户端公开API。它是轻量级的,提供了一个很好的配置文件(没有配置文件),也更容易测试。
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.
您可以在wcf.codeplex.com下载这些位。它包括各种样本,还有一组NuGet包可以帮助您入门。在NuGet上搜索webapi.all。
#2
3
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.
我喜欢这样做的方式是使用RESTful控制器操作。您可以在自己的网站上返回JSON并使用JavaScript调用。其他网站几乎肯定会理解JSON,因此他们可以非常轻松地使用您的API。这比重型WCF层更容易编写和测试。
Check out this question for some example REST frameworks for MVC:
查看此问题以获取MVC的一些示例REST框架:
ASP.NET MVC REST框架
#3
1
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 * including XML and JSON in 30 minutes.
完成数据馈送的一种新方法是使用OData。 Scott Hanselman在为*创建OData API(包括XML和JSON)的30分钟内对它进行了很好的介绍。
It allows you to even throw LINQ queries into your URLs to retrieve exactly the data you need.
它允许您甚至将LINQ查询放入URL中以准确检索所需的数据。
-
Open Data Protocol (Official site)
开放数据协议(官方网站)
-
Open Data Protocol (MSDN, Microsoft)
开放数据协议(MSDN,Microsoft)
#4
0
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.
上次使用它时,WCF JSON绑定非常糟糕。 WCF还附带了各种关于流的疯狂规则以及如何使用[MessageBody]属性。
It was a real PITA to me.
对我来说这是一个真正的PITA。
I knew I've answered something like this before:
我知道我以前回答过这样的话:
What is the best way to implement a RESTful architecture in .NET today?
今天在.NET中实现RESTful架构的最佳方法是什么?
#1
3
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.
您可能想要检查我们在PDC上宣布的新WCF Web API。我们最近发布了一个重大升级。 WCF Web API专门用于允许您以纯HTTP方式向一系列客户端公开API。它是轻量级的,提供了一个很好的配置文件(没有配置文件),也更容易测试。
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.
您可以在wcf.codeplex.com下载这些位。它包括各种样本,还有一组NuGet包可以帮助您入门。在NuGet上搜索webapi.all。
#2
3
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.
我喜欢这样做的方式是使用RESTful控制器操作。您可以在自己的网站上返回JSON并使用JavaScript调用。其他网站几乎肯定会理解JSON,因此他们可以非常轻松地使用您的API。这比重型WCF层更容易编写和测试。
Check out this question for some example REST frameworks for MVC:
查看此问题以获取MVC的一些示例REST框架:
ASP.NET MVC REST框架
#3
1
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 * including XML and JSON in 30 minutes.
完成数据馈送的一种新方法是使用OData。 Scott Hanselman在为*创建OData API(包括XML和JSON)的30分钟内对它进行了很好的介绍。
It allows you to even throw LINQ queries into your URLs to retrieve exactly the data you need.
它允许您甚至将LINQ查询放入URL中以准确检索所需的数据。
-
Open Data Protocol (Official site)
开放数据协议(官方网站)
-
Open Data Protocol (MSDN, Microsoft)
开放数据协议(MSDN,Microsoft)
#4
0
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.
上次使用它时,WCF JSON绑定非常糟糕。 WCF还附带了各种关于流的疯狂规则以及如何使用[MessageBody]属性。
It was a real PITA to me.
对我来说这是一个真正的PITA。
I knew I've answered something like this before:
我知道我以前回答过这样的话:
What is the best way to implement a RESTful architecture in .NET today?
今天在.NET中实现RESTful架构的最佳方法是什么?