3

We are migrating a site from ASP MVC .Net Framework to .Net Core. In many cases we do ajax POST requests from the page to the Controller:

js:

var request = { Name: "bar" };
$.ajax({
    url: "/Home/foo",
    type: "POST",
    data: JSON.stringify(request),
    dataType: 'json',
    contentType: "application/json; charset=utf-8",
    success: function (response) {
        console.log();
    }
});

c# Controller:

[HttpPost]
public async Task<JsonResult> foo(FooRequest request)
{
    //request.Name is empty
    return Json(new { msg = "Success" });
}

c# Model:

public class FooRequest
{
    public string Name { get; set; }
}

This all works in .Net Framework, but fails in Core were all properties of the received object are null.

I've tried using both AddJsonOptions:

services.AddControllersWithViews()
    .AddJsonOptions(option =>
    {
        option.JsonSerializerOptions.PropertyNamingPolicy = null;
        option.JsonSerializerOptions.PropertyNameCaseInsensitive = true;
    });

...and AddNewtonsoftJson:

services.AddControllersWithViews()
    .AddNewtonsoftJson(option =>
    {
        option.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.DefaultContractResolver();
    });

...but none of them works.

What do I need to do to get the same behavior in .Net Core as I do in .Net Framework?

Update

I get the same behavior calling the endoint from Postman (request.Name is null):

POST /home/foo HTTP/1.1
Host: localhost:44393
Content-Type: application/json

{ "Name": "bar" }

I've also tried to serializing the request:

var request = { Name: "bar" };
$.ajax({
    url: "/Home/foo",
    type: "POST",
    data: request,
    dataType: 'json',
    contentType: "application/json; charset=utf-8",
    success: function (response) {
        console.log();
    }
});

...but with the same result

3
  • Should have been more clear, but all properties of the received object are null. Commented Mar 16, 2020 at 19:15
  • 1
    Did you check what you're POSTing? Why are you calling JSON.stringify on the object you're posting? Commented Mar 16, 2020 at 19:23
  • Because that was the only way to get it to work in .Net Framework. I'd be happy to change that, but I get the same issue using data:request, Commented Mar 16, 2020 at 19:26

1 Answer 1

3

Try adding the FromBodyAttribute to your parameter:

[HttpPost]
public async Task<JsonResult> foo([FromBody] FooRequest request)
{
    //request.Name is empty
    return Json(new { msg = "Success" });
}

You can omit this attribute, if you decorate your controller with the ApiControllerAttribute

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks...Just one short question is there a way to set this in the Startup.cs, as I've got a lot of these type of methods (residing my normal Actions)?
Unfortunately no. The only "global"-ish way is to use ApiControllers for Http requests, and normal controllers for views. So if you refactor your api call inside a controller with decorated with [ApiController], you should be able to omit this.
You can probably do it with an action filter.

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.