且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从asp.net 2.0 REST

更新时间:2023-02-11 18:49:17

如果您寻找该模板的REST服务项目,你在说没有开箱即用的解决方案是正确的。然而,REST Web服务使用WCF是可能的。关键部分是定义一个让.NET框架知道该函数没想到的SOAP服务功能时使用几个属性。使用的主要属性是 WebInvoke 属性



下面是一个例子developer.com

  [OperationContract的] 
[WebInvoke(方法=PUT,UriTemplate = /管理/后/(编号))
无效UpdatePost(字符串ID,后后);



将上面的代码实际上是在接口中定义为Web服务。当您创建WCF Web服务项目的接口自动创建。该函数的实际代码将被放置在用于实现Web服务的类。



检查出developer.com文章为一个完整的教程。它可能在第一,如果你的新WCF似乎势不可挡,但你潜入后,我敢肯定你会开始迅速捡东西。这里是链接的artile: http://www.developer.com/net/article的.php / 10916_3695436_1



要回答你所有的问题,



a)在.NET 2.0你应该能够生成使用的 WSE2.0 ,但如果你必须使用.NET 3.5的选项,我会强烈建议去WCF的路线,因为这是很容易,并且设计时考虑了REST。



b)将您的项目不会很难的。这只是一个在您的项目设置目标定位框架的新版本的问题。从WSE2.0服务WCF服务转换的Web服务会有点棘手,虽然。这样做最简单的方法是将代码从各个不同的Web服务功能复制到您实现功能的新版本的类。复制 - 粘贴shinanigans:)



三)我不知道该入门套件是什么,你要引用。 REST风格的Web服务应该在WCF中得到充分的支持这是完全释放为3.5



D)这将有助于了解WCF至少开始前一点,但它不是crutial完全理解它,才能上手。我会建议通过WCF 的 MSDN文章只是阅读至少一次,然后开始工作。我敢肯定,你会遇到其他问题开始,但你可以看看这些部分是你遇到他们。



无论如何,我希望这个信息帮助。祝你好运。



修改



一些改进已经在做REST的世界。正如达雷尔 - 米勒在评论中提到,WCF实际上不是在考虑REST建成。我以前错说话。事实上框架与SOAP内置的头脑和WebInvoke属性填补了这个空白。虽然有很多围绕的话题(网页API VS WCF REST)的争论,的ASP.NET Web API是在.NET中构建REST服务的新选择。我会强烈建议任何人谁读这篇文章,能够在他们的项目中使用.NET 4.5看看它作为一个选项。


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.

This is what I found:

There is NO REST out of box of asp.net.

WCF REST Starter Kit Codeplex Preview 2 base on .net 3.5 and still in beta

Rest ASP.NET Example

REST Web Services in ASP.NET 2.0 (C#)

Exyus

Handling POST and PUT methods with Lullaby

ADO.NET Data Service

...

Now my question,

a) Is a REST solution for .net 2.0? if yes, which one is best solution?

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?

c) WCF REST Starter Kit is good enough to use in production?

d) Do I have to learn WCF first, then WCF REST Starter Kit? where is the best place to start?

I appreciate any help here.

Thanks Wes

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 WebInvoke attribute.

Here is an example from 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.

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

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.

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 :)

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

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 WCF at 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.

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.