C# WCF Restful 服务 GET/POST

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11726209/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 18:58:34  来源:igfitidea点击:

WCF Restful service GET/POST

c#wcfrest

提问by ASR

Can I do something like this?

我可以做这样的事情吗?

[OperationContract]    
[WebInvoke
  (  
    Method = "POST",
    ResponseFormat = WebMessageFormat.Json,
    RequestFormat = WebMessageFormat.Json,
    BodyStyle = WebMessageBodyStyle.Bare,
    UriTemplate = "/abc{integerParam}"
  )
]
ResultStruct abc( int integerParam, CustomClass secondParam );

Idea here being that I can pass first parameter( integer ) in the url, but secondParam comes from POST. Is this even possible?

这里的想法是我可以在 url 中传递第一个参数(整数),但是 secondParam 来自 POST。这甚至可能吗?

I started with WCF REST and not sure about how parameters are assigned. Any pointers will be helpful thank you

我从 WCF REST 开始,不确定如何分配参数。任何指针都会有所帮助谢谢

采纳答案by GSerjo

Yes you can, here is from A Guide to Designing and Building RESTful Web Services

是的,你可以,这里来自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) {...}
    ...
}

As for me, this kind of designing RESTful web services is terrible. This ServiceContrcat is:

对于我来说,这种设计 RESTful Web 服务的方式很糟糕。这个 ServiceContrcat 是:

  • unmaintainable, brittle remote interface
  • Have to create too many methods
  • Polymorphism is absent
  • 不可维护、脆弱的远程接口
  • 必须创建太多方法
  • 多态性不存在

I believe, that remote interface should be stableand flexible, we can use message based approach for designing web services.

我相信,远程接口应该是稳定灵活的,我们可以使用基于消息的方法来设计 Web 服务。

You can find detailed explanation here: Building RESTful Message Based Web Services with WCF, code samples here: Neliburand Nelibur nuget package here

:你可以在这里找到详细的解释构建RESTful基于消息的Web服务与WCF这里,代码示例:Nelibur和Nelibur NuGet包这里