question

JohnnyOlsen-2691 avatar image
0 Votes"
JohnnyOlsen-2691 asked ·

Why Can't .NET Core handle JQuery Ajax Post ?

I have added a simple JQuery Ajax call using POST.
The call is using JSON and the parameters I send are turned into nulls when received in the Controller (MVC)
The same code (except a few things on the controller part are slightly different to meet the specifications regarding JSON) works when I run .NET Framework. But fails on .NET Core. Here is the client code:
var pdata = JSON.stringify({ name:"Johnny Olsen", age:63, SSN:"200757-0409" });
$.ajax({
type: "POST",
url: "home/personInfoPost",
dataType:"json",
contentType: "application/json",
data: pdata,
success: function(res){
$("span").text(res);
}
});
``And server-side code is
like this: [HttpPost]
[Consumes("application/json")]
public string personInfoPost([FromBody] string name, int age, string SSN)
{
var str = name + ", " + age.ToString() + ", " + SSN;
return str + " are Received ok...";
}

Hope you can explain why I have this problem. /Johnny Olsen




dotnet-aspnetcore-mvc
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

cooldadtx avatar image
1 Vote"
cooldadtx answered ·

Model binding in ASP.NET Core is different than ASP.NET as discussed here. Even in ASP.NET your controller doesn't seem like it should work properly.

The binder attempts to match parameters to the received data via the URL, form values, etc. Any that match are assigned to parameters. The FromBody attribute tells the binder to look at the body of the request to match the data. Hence in your case it would look in the body of the request. But the assumption here is that it is a model so the parameter name itself is irrelevant. Since string doesn't have any properties it wouldn't try to match anything. The other 2 parameters are not specified as FromBody, because only 1 parameter supports this, and therefore would never be bound. AFAIK this wouldn't even have worked in ASP.NET but honestly I never would have tried it anyway.

The correct approach to storing data in the body and mapping multiple values to it is to define a model type with the fields inside. Then use the model as a parameter with the FromBody attribute.

public class PersonInfoModel
{
   public string Name { get; set; }
   public int Age { get; set; }
   public string SSN { get; set; }
}

public string personInfoPost ( [FromBody] PersonInfoModel model )
{
}
· 2 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Why Does .NET Core behave differently than .NET Framework?
It works on .NET Framework but not on .NET Core with the same code.

0 Votes 0 ·
cooldadtx avatar image cooldadtx JohnnyOlsen-2691 ·

This has nothing to do with .NET Framework vs Core. It is ASP.NET MVC vs ASP.NET Core, which supported NF until 3.1.

When MVC was originally released the model binder was set up to support the types of scenarios you'd likely see with MVC including using models as parameters. When Web API 2 was added to the library in later years it needed to work differently so a different pipeline was created with its own rules. In APIs we tend to pass data as part of the URL and less as models (like in MVC). Hence the MVC model binder wouldn't work with APIs as well without making potentially breaking changes. Hence API used a separate pipeline that looked similar but behaved differently. This went down to the routing level as well.

In ASP.NET Core both MVC and APIs use the same implementation and now behave the same. This means the behavior is consistent. To get that to work some breaking changes were needed. It is a different platform so that is fine.

0 Votes 0 ·