I'm trying to POST to my ASP.NET Core (RC1) API from jQuery. It works fine when I POST via HttpClient, so I think it's only a stupid thing I'm missing.
I can reproduce my problem on a vanilla default ASP.NET 5 project and this code.
Server
namespace WebApplication1.Controllers
{
[Route("api/[controller]")]
public class ValuesController : Controller
{
[HttpPost]
public IActionResult Post([FromBody]Model input)
{
// input is always null
return Json(input.Value);
}
public class Model
{
public string Value { get; set; }
}
}
}
Client
$.ajax({
url: "http://localhost:5000/api/values",
method: "POST",
contentType: "application/json",
dataType: "json",
data: JSON.stringify({
Value: "some_value"
})
}).done(function(data) {
console.log("Done.", data);
});
I played around by removing [FromBody], that returns a model, but the properties were never populated. I also tried the many solutions and workarounds for similar problems on SO, but I think they don't apply to .NET Core or only solve problems usinf [FromBody] and simple types. For example Web Api Parameter always null or using jQuery post to ASP.Net webapi or http://encosia.com/using-jquery-to-post-frombody-parameters-to-web-api/