是的,你可以,这里来自A Guide to Designing and Building RESTful Web Services
[ServiceContract]
public partial class BookmarkService
{
[WebInvoke(Method = "PUT", UriTemplate = "users/{username}")]
[OperationContract]
void PutUserAccount(string username, User user) {...}
[WebInvoke(Method = "DELETE", UriTemplate = "users/{username}")]
[OperationContract]
void DeleteUserAccount(string username) {...}
[WebInvoke(Method = "POST", UriTemplate = "users/{username}/bookmarks")]
[OperationContract]
void PostBookmark(string username, Bookmark newValue) {...}
[WebInvoke(Method = "PUT", UriTemplate = "users/{username}/bookmarks/{id")]
[OperationContract]
void PutBookmark(string username, string id, Bookmark bm) {...}
[WebInvoke(Method = "DELETE", UriTemplate = "users/{username}/bookmarks/{id}")]
[OperationContract]
void DeleteBookmark(string username, string id) {...}
...
}
对我来说,这种设计 RESTful Web 服务的方式很糟糕。这个 ServiceContrcat 是:
- 不可维护,脆弱的远程接口
- 必须创建太多方法
- 多态性不存在
我相信,远程接口应该是稳定和灵活的,我们可以使用基于消息的方法来设计 Web 服务。
你可以在这里找到详细的解释:Building RESTful Message Based Web Services with WCF,代码示例在这里:Nelibur and Nelibur nuget package here