调用 mvc 控制器方法和 WCF WEB API Rest 服务调用有什么区别?
调用 mvc 控制器方法和 WCF WEB API Rest 服务调用有什么区别?
我可以创建一个 mvc 控制器 post 方法,该方法将允许我执行我需要的任何代码。
我还可以使用 MVC 创建 WCF Web API REST 服务。
这两种访问数据的方法有什么区别?
更具体地说,在这种情况下使用 WCF 有哪些优势?
What is the difference between making calls to mvc controller methods and WCF WEB API Rest Service Calls?
I can create an mvc controller post method that will allow me to execute any code i need.
I can also create a WCF Web API REST Service with MVC.
What is the difference between these two approaches to accessing data ?
More specifically what are the advantages of utilizing WCF in this scenario ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
WCF Web API 可以开箱即用地处理 XML 和 JSON,而在使用 MVC 时,您必须“手动”创建 XML 和 JSON(阅读:使用序列化器)——这只是 WCF Web API 相对于 MVC 的优势之一。
另一个是 WCF Web API 提供的分离级别,例如实现服务逻辑与内容协商。
然而,您可以轻松使用 IoC 容器并对使用 WCF Web API 创建的 API 进行单元测试。
WCF Web API 主要是为了构建 ReSTful API 而创建的,而 MVC 也只允许创建它们 - 因此,使用 WCF Web API,您会更轻松地创建 ReST API。
如果您计划从头开始作为 Web (ReST) Api 项目,则应该从 WCF Web API 开始。
如果您计划从一个也提供 ReSTful 服务的网站开始,您应该建议 MVC。
您还应该关注 Glenn Block 的这条推文。
WCF Web API handles XML and JSON out of the box whereas you'll have to create your XML and JSON "by hand" (read: using the Serializers) when using MVC - this is only one of the benefits of WCF Web API over MVC.
Another one is the level of separation WCF Web API offers e.g. implementing your service logic vs. content negotiation.
Yet you can easily use IoC containers and unit test your APIs being created using WCF Web API.
WCF Web API mainly has been created to build ReSTful API's whereas MVC just allows it to create them too - thus with WCF Web API you'll feel more comfortable creating ReST APIs.
If you're planning to start a project from scratch as an Web (ReST) Api, you should start with WCF Web API.
If you're planning to start with a Website that also offers ReSTful Services, you should suggest MVC.
You should also regard this tweet from Glenn Block.
您可以使用其中任何一个来完成典型的 REST API。
通常问题归结为(1)您需要哪些具体功能以及(2)您更熟悉哪些技术。
WCF 的一些功能非常简洁,但在 MVC 中不可用(例如二进制序列化、无需 IIS 进行托管的能力等),但这些功能通常不是 REST API 中的要求。
总的来说,我会说:
如果您已经有了 WCF 实现并希望将其公开为 REST API,请使用 WCF。
如果您已经有一个 MVC 站点并希望将其公开为 MVC 的 REST API。
You could accomplish a typical REST API with either one.
Usually the issue boils down to (1) what specific features you need and (2) what technology you are more familiar with.
There are some features of WCF that are really neat and not available in MVC (like binary serializations, the ability to host without IIS, et cetera) but those are not typically requirements in a REST API.
Overall I would say:
If you have an WCF implementation already in place and want to expose it as a REST API go for it with WCF.
If you already have an MVC site and want to expose it as a REST API stick with MVC.