2

I have a client which is sending a json to my asp.net mvc application. Where can I receive the json body?

sending:

        var client = new RestClient(uri);
        client.Authenticator = new NtlmAuthenticator();

        RestRequest requestCom =
            new RestRequest("", method);

        //add headers
        requestCom.AddHeader("Accept", "application/json");

        if (body != null)
        {
            requestCom.AddJsonBody(body);
        }

        IRestResponse response = client.Execute(requestCom);

controller:

    public string Index([FromBody]object body)
    {
        return body.ToString();
    }

The url is my controller in the mvc application. So how can i receive the body?

6

1 Answer 1

3

Lets say you have a model like this

public class MyModel {
    public string AProperty { get; set; }
}

And send to the server

var client = new RestClient(uri);
client.Authenticator = new NtlmAuthenticator();

var requestCom = new RestRequest("", method);
//add headers
requestCom.AddHeader("Accept", "application/json");

var body = new MyModel {
    AProperty = "Hello World!!!"
}

if (body != null) {
    requestCom.AddJsonBody(body);
}

IRestResponse response = client.Execute(requestCom);

The Controller action would have to expect the model in the body

public string Index([FromBody]MyModel body) {
    return body.ToString();
}
0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.