本文介绍了为什么要使用ASP.NET MVC 2的REST服务?为什么不WCF?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我看到MVC 2现在支持 [HttpPut] [HttpDelete] 以及 [HTTPGET] [HttpPost] ,从而可以用它做一个全面的RESTful Web服务。

So I see that MVC 2 now supports [HttpPut] and [HttpDelete] as well as [HttpGet] and [HttpPost], making it possible to do a full RESTful Web service using it.

我一直在使用WCF REST的工具了一会儿,觉得相当强大,但我很想找出什么(如果有的话)的优点有使用MVC 2的方法。

I've been using the REST toolkit for WCF for a while and find it fairly powerful, but I'd be interested to find out what (if any) advantages there are using the MVC 2 approach.

链接,战争的故事,甚至是纯粹的道听途说的欢迎。

Links, war stories, or even pure hear-say are welcome.

推荐答案

我是pretty自年初以来确定ASP.NET MVC已经支持所有的HTTP动词。至少不得不从他们的开始。在V2的新的唯一的事情是,他们的属性。

I'm pretty sure ASP.NET MVC has supported all the HTTP verbs since the beginning. At least the HttpVerb Enumeration has had them from the beginning. The only thing that's new in V2 is that they are attributes.

// V1
[AcceptVerbs( HttpVerbs.Delete )]

// V2
[HttpDelete]

一六,一半的其他十几个。至于是否要公开通过WCF或ASP.NET MVC的功能,它会回落到你是怎么想您的应用程序。

Six of one, half a dozen of the other. As to whether you want to expose functionality through WCF or ASP.NET MVC, it would come down to how you think of your application.


  • 如果你认为它是刚刚发生在JavaScript来编写,并呼吁到RESTful服务的胖客户端应用程序的数据(然后将其格式化客户端),那么WCF会觉得像一个更正确的解决方案(尽管你可以使用任何这样做)。

  • If you think of it as a thick client app that just happens to be written in JavaScript and calls out to restful services for data (then formats it client side) then WCF would feel like a more correct solution (even though you could do it using either).

不过,如果你认为你的应用程序返回的内容某种形式的消费,然后使用自己的行为一个RESTful API会更有意义的服务器应用程序。你的行为将返回,将没有必要进行进一步的处理中显示在浏览器完全格式化的内容。您的可能的回报格式的内容(HTML或其他方式)的Web服务,但会以某种感觉不对劲。

However if you think of your application as a server app that returns content in some form or another for consumption, then using a RESTful API for your actions would make more sense. Your actions would return fully formatted content that would be displayed in the browser without a need for further processing. You could return formatted content (HTML or otherwise) from a web service, but that would somehow feel wrong.

至少那种区分是有意义的在我的脑海=)。您还可能有兴趣在菲尔哈克对的。

At least that kind of distinction makes sense in my head =). You may also be interested in Phil Haack's post on How a Method Becomes an Action.

现在有另一种选择,。 在他的博客的简单介绍,并有一个有趣的博客文章讨论了使用Web API和控制器的。

There's now another option, Web API. ScottGu has a brief introduction in his blog and there's an interesting blog post discussing creating APIs using the Web API vs controllers here.

这篇关于为什么要使用ASP.NET MVC 2的REST服务?为什么不WCF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 09:12