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.

Community
  • 1
  • 1
Kolichikov
  • 2,944
  • 31
  • 46
  • You are not sending back the content in the response. `return Ok(documentContents);` – sudheeshix Dec 07 '16 at 17:12
  • It's an empty string in the controller. My problem is that the data that comes to me is null. – Kolichikov Dec 07 '16 at 17:19
  • Does your application work on local before running in service fabric? – Win Dec 07 '16 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. – Kolichikov Dec 07 '16 at 17:23
  • What happens if you remove the `[FromBody]` attribute? – Ryan Erdmann Dec 07 '16 at 17:49
  • Well, the operations parameter is still null, BUT I can now read from the body directly. I guess I didn't try the stream code without the frombody tag. Feels slightly hacky (I'd like to eventually bind to the object directly in the parameter), but at this point I'll take it. Still not sure why it doesn't work, but if you post it as an answer I'll mark it as accepted. – Kolichikov Dec 07 '16 at 18:02

2 Answers2

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; }
}
Carlos Crespo
  • 256
  • 1
  • 7
  • 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. – Kolichikov Dec 07 '16 at 17:54
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("");
    }
bvoleti
  • 2,472
  • 1
  • 18
  • 20