C# 来自 asp.net 2.0 的 REST
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/691111/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
REST from asp.net 2.0
提问by weslleywang
I just built a asp.net 2.0 web site. Now I need add REST web service so I can communicate with another web application. I've worked with 2 SOAP web service project before, but have no experise with REST at all. I guess only a coupleweeks would works fine. after googling, I found it's not that easy.
我刚刚建立了一个asp.net 2.0 网站。现在我需要添加 REST Web 服务,以便我可以与另一个 Web 应用程序进行通信。我以前使用过 2 个 SOAP Web 服务项目,但根本没有 REST 经验。我想只有几周才能正常工作。谷歌搜索后,我发现这并不容易。
This is what I found:
这是我发现的:
There is NO REST out of box of asp.net.
没有 REST 开箱即用的 asp.net。
WCF REST Starter Kit Codeplex Preview 2base on .net 3.5 and still in beta
WCF REST Starter Kit Codeplex Preview 2基于 .net 3.5 并且仍处于测试阶段
REST Web Services in ASP.NET 2.0 (C#)
ASP.NET 2.0 (C#) 中的 REST Web 服务
Handling POST and PUT methods with Lullaby
...
...
Now my question,
现在我的问题,
a) Is a REST solution for .net 2.0? if yes, which one is best solution?
a) 是 .net 2.0 的 REST 解决方案吗?如果是,哪一个是最好的解决方案?
b) if I have to, how hard to migrate my asp.net from 2.0 to 3.5? is it as simple as just compile, or I have to change a lot code?
b) 如果必须,将我的 asp.net 从 2.0 迁移到 3.5 有多难?是编译这么简单,还是要改很多代码?
c) WCF REST Starter Kit is good enough to use in production?
c) WCF REST Starter Kit 是否足以用于生产?
d) Do I have to learn WCF first, then WCF REST Starter Kit? where is the best place to start?
d) 我是否必须先学习 WCF,然后再学习 WCF REST Starter Kit?最好的起点是哪里?
I appreciate any help here.
我很感激这里的任何帮助。
Thanks Wes
谢谢韦斯
采纳答案by regex
If your looking for a project that templates a REST service, you're correct in saying there is no out of the box solution. However, RESTful web services are possible using WCF. The key part is to use several attributes when defining your service functions that let the .NET framework know that the function is not expecting SOAP. The main attribute to use is the WebInvokeattribute.
如果您正在寻找一个为 REST 服务模板化的项目,那么您说没有现成的解决方案是正确的。但是,可以使用 WCF 实现 RESTful Web 服务。关键部分是在定义服务函数时使用多个属性,让 .NET 框架知道该函数不需要 SOAP。要使用的主要属性是WebInvoke属性。
Here is an example from developer.com:
这是来自developer.com的示例:
[OperationContract]
[WebInvoke(Method = "PUT", UriTemplate = "/admin/post/{id}")]
void UpdatePost(string id, Post post);
The above code will actually be defined in an interface for your web service. The interface is created automatically when you create your WCF web service project. The actual code for the function will be placed in the class used to implement the web service.
上面的代码实际上将在您的 Web 服务的接口中定义。当您创建 WCF Web 服务项目时,会自动创建该接口。该函数的实际代码将放置在用于实现 Web 服务的类中。
Check out the article on developer.com for a full tutorial. It might seem overwhelming at first if your new to WCF, but after you dive into it, I'm sure you'll start to pick things up quickly. Here is the link for the artile: http://www.developer.com/net/article.php/10916_3695436_1
查看 developer.com 上的文章以获取完整教程。如果您刚开始接触 WCF,一开始可能会让人不知所措,但是在您深入了解之后,我相信您会很快上手。这是文章的链接:http: //www.developer.com/net/article.php/10916_3695436_1
To answer all of your questions,
回答你所有的问题,
a) In .NET 2.0 you should be able to build RESTful services using WSE2.0, but if you have the option to use .NET 3.5, I would strongly recommend going the route of WCF since it is much easier and is designed with REST in mind.
a) 在 .NET 2.0 中,您应该能够使用WSE2.0构建 RESTful 服务,但是如果您可以选择使用 .NET 3.5,我强烈建议您使用 WCF 的路线,因为它更容易并且是用 REST 设计的心里。
b) Converting your project won't be hard at all. It's just a matter of targetting the new version of the framework in your project settings. Converting a web service from a WSE2.0 service to a WCF service will be a bit trickier though. The easiest way to do so would be to copy the code from each of the different web service functions into the class where you implement the new version of the function. Copy-Paste shinanigans :)
b) 转换您的项目一点也不难。这只是在您的项目设置中定位新版本框架的问题。不过,将 Web 服务从 WSE2.0 服务转换为 WCF 服务会有点棘手。最简单的方法是将代码从每个不同的 Web 服务函数复制到您实现新版本函数的类中。复制粘贴 shinanigans :)
c) I'm not sure what this starter kit is that you're referring to. RESTful web services should be fully supported in WCF which was fully released as of 3.5
c) 我不确定您所指的入门套件是什么。WCF 应完全支持 RESTful Web 服务,WCF 从 3.5 开始完全发布
d) It would be helpful to understand WCF at least a little before beginning, but it's not crutial to understand it completely in order to get started. I would recommend just reading through the MSDN article on WCFat least once, and then begin working. I'm sure you will come across other questions as you begin, but you can look up those parts as you come across them.
d) 在开始之前至少了解一点 WCF 会有所帮助,但为了开始,完全理解它并不重要。我建议至少通读一次有关 WCF的MSDN 文章,然后开始工作。我相信您在开始时会遇到其他问题,但是您可以在遇到这些部分时查找它们。
Anyway, I hope this information helps. Good luck to you.
无论如何,我希望这些信息有帮助。祝你好运。
Edit
编辑
Some improvements have been made in the REST world. As Darrel Miller mentioned in the comments, WCF was not in fact built with REST in mind. I mis-spoke previously. In fact the framework is built with SOAP in mind and the WebInvoke attribute fills the gap. Although there is a lot of debate around the topic (Web API vs WCF REST), ASP.NET Web API is a new option for building REST services in .NET. I would strongly recommend that anyone who reads this post and is able to use .NET 4.5 in their project look into it as an option.
在 REST 世界中进行了一些改进。正如 Darrel Miller 在评论中提到的,WCF 实际上并没有考虑到 REST。我之前说错了。事实上,该框架是在考虑 SOAP 的情况下构建的,而 WebInvoke 属性填补了这一空白。尽管围绕该主题存在很多争论(Web API 与 WCF REST),但 ASP.NET Web API 是在 .NET 中构建 REST 服务的新选项。我强烈建议阅读这篇文章并能够在他们的项目中使用 .NET 4.5 的任何人都将其视为一种选择。
回答by Jesse Dearing
I'm not sure about a REST solution for 2.0. So I think WCF is going to be the way you'll have to go with this one. MSDN has a long introduction to it.
我不确定 2.0 的 REST 解决方案。所以我认为 WCF 将成为你必须采用的方式。 MSDN 有很长的介绍。
My personal philosophy is that if a technology is still in beta then I would rather not use it for something that will potentially be a production application so I would start with WCF since it was introduced in the 3.0 version of the framework.
我个人的理念是,如果一项技术仍处于测试阶段,那么我宁愿不将它用于可能成为生产应用程序的东西,因此我将从 WCF 开始,因为它是在框架的 3.0 版本中引入的。
3.0 and 3.5 is actually an update to the libraries, but the CLR is still 2.0 so if the libraries you're using are not deprecated or changed in the new frameworks it should be okay.
3.0 和 3.5 实际上是对库的更新,但 CLR 仍然是 2.0,所以如果您使用的库在新框架中没有被弃用或更改,那应该没问题。
回答by tvanfosson
You could look at using ASP.NET MVC as a RESTful web services platform. WCF is probably the way to go in the long run, but MVC should easily handle it. Your actions would just need to be set up to return JSON or XML, depending on how you want to serialize it. MVC offers both a JsonResult and fully customizable ContentResult -- i.e., you serialize your response to a string property on the result and set it's type and encoding.
您可以考虑使用 ASP.NET MVC 作为 RESTful Web 服务平台。从长远来看,WCF 可能是要走的路,但 MVC 应该很容易处理它。您的操作只需要设置为返回 JSON 或 XML,具体取决于您希望如何对其进行序列化。MVC 提供了 JsonResult 和完全可定制的 ContentResult —— 即,您将您的响应序列化为结果的字符串属性并设置它的类型和编码。
NOTE: MVC does require 3.5 SP1 so it's not going to be a 2.0 solution. If you require 2.0, you'll need to look elsewhere.
注意:MVC 确实需要 3.5 SP1,因此它不会是 2.0 解决方案。如果您需要 2.0,则需要查看其他地方。
回答by Pontus Gagge
Take a look at this CodeProjectfor a starting point. It seems somewhat... hacky. Modernizing to WCFwould be cleaner.
以这个 CodeProject为起点。这似乎有点...... hacky。现代化到WCF会更干净。
回答by david valentine
REST, or RESTFUL Adding this to the System.Web in web.config will enable http get, and put.. aka REST-Like... but not full rest.
REST 或 RESTFUL 将此添加到 web.config 中的 System.Web 将启用 http get 和 put.. aka REST-Like...但不是完全休息。
<System.Web>
<webServices>
<protocols>
<add name="HttpSoap"/>
<add name="HttpPost"/>
<add name="HttpGet"/>
</protocols>
</webServices>
</System.Web>
回答by Cheeso
Use WCF REST.
使用 WCF REST。
You can use it today. The WCF REST library is ready to go, usable in production.
您今天可以使用它。WCF REST 库已准备就绪,可用于生产。
The WCF Starter Kit (see http://msdn.microsoft.com/en-us/netframework/cc950529.aspx) is something else. It delivers extra stuff; it includes a set of VS project templates, docs, samples and a few tools for building REST apps (client or server) with WCF. One cool tool is the "Paste Xml as Type" command add-in to Visual Studio.
WCF 入门工具包(请参阅http://msdn.microsoft.com/en-us/netframework/cc950529.aspx)是另一回事。它提供额外的东西;它包括一组 VS 项目模板、文档、示例和一些用于使用 WCF 构建 REST 应用程序(客户端或服务器)的工具。一个很酷的工具是 Visual Studio 中的“将 Xml 粘贴为类型”命令加载项。
On the other hand, building a REST client, in the general case, I'd recommend taking advantage of the HttpClient assembly in the starter kit (Microsoft.Http.dll). It's small, simple, and useful. A low-risk and high-value dependency even at the current "preview" status of the Starter Kit.
另一方面,在构建 REST 客户端时,在一般情况下,我建议利用入门工具包 (Microsoft.Http.dll) 中的 HttpClient 程序集。它小巧、简单且有用。即使在 Starter Kit 的当前“预览”状态下也是低风险和高价值的依赖项。
If you were consuming just a single REST service, then you don't need the general Microsoft.Http.dll assembly; you can do all the stuff it does in some code that uses WebRequest. But if you wanted a more general flexible class for manipulating client-side REST requests, then grab that Microsoft.Http.dll.
如果您只使用一个 REST 服务,那么您不需要通用的 Microsoft.Http.dll 程序集;你可以在一些使用 WebRequest 的代码中完成它所做的所有事情。但是,如果您想要一个更通用的灵活类来处理客户端 REST 请求,那么请获取 Microsoft.Http.dll。
回答by SerialSeb
If you want a framework built with REST in mind, you should have a look at OpenRasta...
如果你想要一个用 REST 构建的框架,你应该看看 OpenRasta ...