C# Rest service - unable to receive JSON request inside service method -


i have rest service accepts json data client. able retrieve json input client using .net class. want retrieve data in json string format, not class object.

this have tried far.

input json

<input id="text3" type="text" value='{ "searchby": "pending cases", "displayoptions": [ {"producers": "yes", "ga/bga/firm": "yes"}],"userid": "xxx", "userauthtoken": "0000" }' /></p> 

calling service via javascript

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));             }         });      } 

service side - contract

[operationcontract]     [webinvoke(         method = "post",         bodystyle = webmessagebodystyle.bare,         requestformat = webmessageformat.json,         responseformat = webmessageformat.json)]     string searchpredefined(predefinedsearchrequestmodel predefinedsearchrequest); 

implementation of service method

public string searchpredefined(predefinedsearchrequestmodel predefinedsearchrequest)     {         string outputstr = "predefinedsearchrequest object gets populated here ";         return outputstr;     } 

model class

[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; } } 

so far these code works fine. when client calls service json data, service method gets hit , model object populated.

however need call 3rd party service same json string service. why need input data in raw json/string format, not c# object.

how can data in server side json string ?

i recommend serializing object in searchpredefined using newtonsoft , passing result through:

var serializedobject = newtonsoft.json.jsonconvert.serializeobject<predefinedsearchrequestmodel>(predefinedsearchrequest); 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -