在 WCF Web 服务中传递字符串数组

Passing String Array In WCF Web Services

我的网络服务现在看起来像这样:

        [WebInvoke(UriTemplate = "/GetAllAreaNotes", Method = "GET", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
        [OperationContract, CorsEnabled]
        [Description("Request to get all area note")]
        AreaNote[] GetAllAreaNotes();

        [WebInvoke(UriTemplate = "/GetAreaNotesByIDs", Method = "GET", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
        [OperationContract, CorsEnabled]
        [Description("Request to get area notes by IDs")]
        AreaNote[] GetAreaNotesByIDs();

/GetAllAreaNotes 已在运行。 /GetAreaNotesByIDs 应该类似,但我不确定如何在此 Web 服务中传递字符串数组。这是我的问题:

  1. 如果这仍然是一个 Get 请求,可以将字符串数组作为 URL?
  2. 的一部分传递
  3. 或者,我应该改为 POST 请求?

我们不能将字符串数组作为 URL 的一部分传递。如果要通过URL传递参数,参数类型只能是字符串。

解决方法是使用POST方法:

    [WebInvoke(UriTemplate = "/GetAllAreaNotes", Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
    string[] GetAllAreaNotes(string[] test);

我们可以启用WCF中的帮助文档:

           <endpointBehaviors>
                <behavior name="ESEndPointBehavior">
                    <webHttp helpEnabled="true"/>
                </behavior>
            </endpointBehaviors>

在帮助文档中可以看到服务的请求格式和响应格式。