1

I want to create a WCF rest service which accept a json data and parse all value.

My json data coming from client side like this:

{"user":{"UserName":"123","Pass":"123"}}

i create a simple wcf OperationContract :

 [OperationContract]
        [WebInvoke(Method = "POST",
            UriTemplate = "Login",
            BodyStyle = WebMessageBodyStyle.Wrapped,
            ResponseFormat = WebMessageFormat.Json,
            RequestFormat = WebMessageFormat.Json
          )]
        string Login(User user);

what can i do in this login method for parsing the json data??

1
  • Does this work? Have you tried it? What response do you get? Commented Apr 7, 2017 at 6:13

1 Answer 1

1

If you send data from your client like this:

{"UserName":"123","Pass":"123"}

You have to like this class

class User{

public string Username {get;set;}
public string Pass {get;set;}
}

If your client insist on this request :

{"user":{"UserName":"123","Pass":"123"}},

You should be have like this class:

class User{

public string Username {get;set;}
public string Pass {get;set;}
}

class RequestJ{
public User user{get;set;}
}

And you must change your wcf service like this :

[OperationContract]
        [WebInvoke(Method = "POST",
            UriTemplate = "Login",
            BodyStyle = WebMessageBodyStyle.Wrapped,
            ResponseFormat = WebMessageFormat.Json,
            RequestFormat = WebMessageFormat.Json
          )]
        string Login(RequestJ user);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.