Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/331.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#Rest服务-无法在服务方法内接收JSON请求_C#_Web Services_Rest_Wcf_Wcf Rest - Fatal编程技术网

C#Rest服务-无法在服务方法内接收JSON请求

C#Rest服务-无法在服务方法内接收JSON请求,c#,web-services,rest,wcf,wcf-rest,C#,Web Services,Rest,Wcf,Wcf Rest,我有一个REST服务,它接受来自客户端的JSON数据。我能够使用.Net类从客户端检索JSON输入。但我希望以JSON字符串格式检索数据,而不是作为类对象 这就是我迄今为止所尝试的 输入JSON <input id="Text3" type="text" value='{ "searchBy": "Pending Cases", "displayOptions": [ {"producers": "yes", "GA/BGA/Firm": "yes"}],"userId": "xxx",

我有一个REST服务,它接受来自客户端的JSON数据。我能够使用.Net类从客户端检索JSON输入。但我希望以JSON字符串格式检索数据,而不是作为类对象

这就是我迄今为止所尝试的

输入JSON

<input id="Text3" type="text" value='{ "searchBy": "Pending Cases", "displayOptions": [ {"producers": "yes", "GA/BGA/Firm": "yes"}],"userId": "xxx", "userAuthToken": "0000" }' /></p>
服务方-合同

function CallService()
    {
        var inputJSON = $("#Text3").val();
        var endpointAddress = $("#Text1").val();
        var url = endpointAddress + $("#Text2").val();

        $.ajax({
            type: 'POST',
            url: url,
            contentType: 'application/json',
            data: inputJSON,
            success: function (result) {                    
                $("#Text4").val(" " + JSON.stringify(result));
            }
        });

    }
[OperationContract]
    [WebInvoke(
        Method = "POST",
        BodyStyle = WebMessageBodyStyle.Bare,
        RequestFormat = WebMessageFormat.Json,
        ResponseFormat = WebMessageFormat.Json)]
    string SearchPredefined(PredefinedSearchRequestModel PredefinedSearchRequest);
服务方法的实施

public string SearchPredefined(PredefinedSearchRequestModel PredefinedSearchRequest)
    {
        string outputStr = "PredefinedSearchRequest Object gets successfully populated here ";
        return outputStr;
    }
模型类

[DataContract]
public class PredefinedSearchRequestModel
{
    [DataMember]
    public string searchBy { get; set; }

    [DataMember]
    public List<displayOptionsModelPredefined> displayOptions { get; set; }

    [DataMember]
    public string userId { get; set; }

    [DataMember]
    public string userAuthToken { get; set; }

}

[DataContract]
[Serializable]
public class displayOptionsModelPredefined
{
    [DataMember]
    public string producers { get; set; }

    [DataMember(Name="GA/BGA/Firm")]
    public string firm { get; set; }
}
[DataContract]
公共类预定义的SearchRequestModel
{
[数据成员]
公共字符串searchBy{get;set;}
[数据成员]
公共列表显示选项{get;set;}
[数据成员]
公共字符串用户标识{get;set;}
[数据成员]
公共字符串userAuthToken{get;set;}
}
[数据合同]
[可序列化]
公共类显示选项模型预定义
{
[数据成员]
公共字符串生成器{get;set;}
[数据成员(Name=“GA/BGA/Firm”)]
公共字符串公司{get;set;}
}
到目前为止,这些代码运行良好。当客户机使用JSON数据调用我的服务时,会命中服务方法并成功填充模型对象

但是,我需要使用我的服务中的相同JSON字符串调用另一个第三方服务。这就是为什么我需要原始JSON/字符串格式的输入数据,而不是C#对象


如何将服务器端的数据作为JSON字符串获取

我建议使用NewtonSoft序列化SearchPredefined中的对象,并将结果传递给:

var serializedObject = Newtonsoft.Json.JsonConvert.SerializeObject<PredefinedSearchRequestModel>(PredefinedSearchRequest);
var serializedObject=Newtonsoft.Json.JsonConvert.SerializeObject(预定义的SearchRequest);