Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WCF JSON POST请求,单个字符串参数未绑定并返回400_C#_.net_Json_Wcf_Rest - Fatal编程技术网

C# WCF JSON POST请求,单个字符串参数未绑定并返回400

C# WCF JSON POST请求,单个字符串参数未绑定并返回400,c#,.net,json,wcf,rest,C#,.net,Json,Wcf,Rest,在我的WCF(azure云)服务中,我希望支持JSON。我正在创建一些测试方法,看看是否一切正常。我可以让get调用正常工作,但当我用一个简单的参数发布帖子时,我总是会得到: The remote server returned an error: (400) Bad Request. 如果我不发送一个参数,它将执行该方法,当然参数是空值。我尝试了JSON和WebMessageBodyStyle的不同格式,但似乎都不起作用 如果我将参数类型更改为Stream,我将接收数据,但我必须手动对其进行

在我的WCF(azure云)服务中,我希望支持JSON。我正在创建一些测试方法,看看是否一切正常。我可以让get调用正常工作,但当我用一个简单的参数发布帖子时,我总是会得到:

The remote server returned an error: (400) Bad Request.
如果我不发送一个参数,它将执行该方法,当然参数是空值。我尝试了JSON和WebMessageBodyStyle的不同格式,但似乎都不起作用

如果我将参数类型更改为Stream,我将接收数据,但我必须手动对其进行反序列化。这应该没必要吧

接口:

        [OperationContract]
        [WebInvoke(UriTemplate = "Test",
            Method = "POST", 
            BodyStyle = WebMessageBodyStyle.WrappedRequest,
            RequestFormat = WebMessageFormat.Json,
            ResponseFormat = WebMessageFormat.Json)]
        string Test(string data);
Impl:

测试客户端:

            WebClient client = new WebClient();
            client.Headers["Content-type"] = "application/json";
            client.Encoding = System.Text.Encoding.UTF8;
            string jsonInput = "{'data':'testvalue'}";
            string postResponse = client.UploadString(postUrl, jsonInput);
            Console.WriteLine("post response: " + postResponse);

黄金组合是在JSON代码中结合WebMessageBodyStyle.WrappedRequest使用双引号

工作JSON:

   string jsonInput = "{\"data\":\"testvalue\"}";
将WebMessageBodyStyle设置为Bare时,以下JSON起作用:

   string jsonInput = "\"testvalue\"";

非常感谢你
   string jsonInput = "\"testvalue\"";