1

This is my first asp.net core app, so I'm probably missing something obvious, but I've looked through the other StackOverflow answers, and the solutions there haven't helped me. I have an asp.net core mvc app that I'm running on service fabric, and I'm trying to post a string to a controller. The string is originally an array of json objects.

My ajax post:

var sendThis = { "operations": JSON.stringify(operations) };
$.ajax({
    url: '/Home/Execute',
    type: 'POST',
    data: sendThis,
    dataType: "json",
    contentType: 'application/json; charset=utf-8',
    error: function (xhr) {
        $("#save-footer-text").val("Saving failed. Please contact an administrator");
    },
    success: function (result) {
        $(".save-footer").addClass("slider");
    },
    async: true
});

My controller on the other side. I took the stream stuff from another stack overflow answer, but it just returns an empty string when it's done.

[HttpPost]
public IActionResult Execute([FromBody] string operations /*this is null*/)
{
    var i = 5;
    string documentContents; //this will be an empty string.
    Request.Body.Position = 0;
    using (Stream receiveStream = Request.Body)
    {
        using (StreamReader readStream = new StreamReader(receiveStream, Encoding.Unicode))
        {
            documentContents = readStream.ReadToEnd();
        }
    }
    Console.WriteLine(i);
    return new OkResult();
}

From the other stackoverflow answers, I've also tried posting with traditional set to to true, and I've tried posting operations into a model, like this

public class M
{
    public string status { get; set; }
    public string appName { get; set; }
    public string startTime { get; set; }
    public string endTime { get; set; }
    public string description { get; set; }
    public string operation { get; set; }
}

with the controller changed to public IActionResult Execute([FromBody] List<M> operations)

I've checked that my javascript does send a request, with Chrome tools reporting that the Request payload is:

operations= my json string here

I also see this in Fiddler, so I know it is going over the wire.

Per this answer, I've also tried updating JsonSettings, but that didn't help either.

I'm using 1.0.0-rc2-final for the asp.net core package, and 5.1.150 for service fabric.

What am I missing here? Sorry if the answer is really trivial. Thank you for any help.

6
  • You are not sending back the content in the response. return Ok(documentContents); Commented Dec 7, 2016 at 17:12
  • It's an empty string in the controller. My problem is that the data that comes to me is null. Commented Dec 7, 2016 at 17:19
  • Does your application work on local before running in service fabric? Commented Dec 7, 2016 at 17:20
  • Not sure. Visual Studio tries to enter debugging mode and just freezes up. I haven't tried just creating a core app without service fabric. I'll investigate that. I should point out that all the gets and the rest of the app do work. Commented Dec 7, 2016 at 17:23
  • What happens if you remove the [FromBody] attribute? Commented Dec 7, 2016 at 17:49

2 Answers 2

0

If the operations variable in the script is an array

    var operations = new Array();

    var op1 = { status: 's1', appName: 'a1', startTime: 'st1', endTime: 'e1', description: 'd1', operation: 'o1' };
    operations.push(op1);

    var op2 = { status: 's2', appName: 'a2', startTime: 'st2', endTime: 'e2', description: 'd21', operation: 'o2' };
    operations.push(op2);

    var sendThis = JSON.stringify(operations);
    ...// ajax call here

And the controller post method is defined as follows then List<M> operations should contain the values sent over the wire as expected.

    [HttpPost]
    public IActionResult Execute([FromBody]List<M> operations)
    {
        // ... some code here
        return Json("");
    }
Sign up to request clarification or add additional context in comments.

Comments

0

It is a list of M?

If it is, then your json shouldn't be like this

var sendThis = { "operations": JSON.stringify(operations) }; 

try this:

var sendThis = JSON.stringify(operations);

I think asp.net is trying to deserialize an object like this one:

public class Operation
{
    public List<M> Operations { get; set; }
}

1 Comment

I have tried this when using the model binding approach. I've also tried not using stringify (and passing in the actual object), but both approaches haven't worked.

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.