[OperationContract] [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest, UriTemplate = "Testmethod/JSON/")] bool TestmethodJson(Complain comp);
public bool TestmethodJson(Complain comp) { if (comp != null) { //code goes here return true; } else { //code goes here return false; }
static void Main(string[] args) { Complain comp = new Complain() { CompainType = "type1", CompainBody = "Body1" }; //JavaScriptSerializer serializer = new JavaScriptSerializer(); string output = JsonConvert.SerializeObject(comp); //string output = serializer.Serialize(complain); string strUri = "http://localhost:35798/NavGAT.svc/Testmethod/JSON/"; Uri uri = new Uri(strUri); WebRequest request = WebRequest.Create(uri); request.Method = "POST"; request.ContentLength = output.Length; request.ContentType = "application/json; charset=utf-8"; // JavaScriptSerializer jsonSerializer = new JavaScriptSerializer(); string serOut = JsonConvert.SerializeObject(comp); // string serOut = jsonSerializer.Serialize(complain); using (StreamWriter writer = new StreamWriter(request.GetRequestStream())) { writer.Write(serOut); } WebResponse responce = request.GetResponse(); Stream reader = responce.GetResponseStream(); StreamReader sReader = new StreamReader(reader); string outResult = sReader.ReadToEnd(); sReader.Close(); }
using System.Runtime.Serialization; [DataContract] public class Complain { #region Private Members private string _CompainBody ; private string _CompainType ; #endregion #region Public Members [DataMember] public int CompainBody { get { return _CompainBody ; } set { _CompainBody = value; } } [DataMember] public string CompainType { get { return _CompainType ; } set { _CompainType = value; } } } }
BodyStyle = WebMessageBodyStyle.WrappedRequest
{ "comp": { "ComplainType": "1", "ComplainBody": "message" } }
You have BodyStyle as WrappedRequest,so you need to do- Either remove Hide Copy Code BodyStyle = WebMessageBodyStyle.WrappedRequest or pass your object like Hide Copy Code { "comp": { "ComplainType": "1", "ComplainBody": "message" } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)