I am having difficulty with my POST request to an ASP.NET Core 3 server because the number
always seems to be 0
. Is there something specific in ASP.NET Core 3 that I might be missing?
Using the [FromBody]
parameter has not resolved the issue. Even passing classes as parameters does not seem to change the values.
[HttpPost]
[Route("/api/add")]
public ActionResult<Dictionary<string, object>> Add(int number)
{
// the value of 'number' is consistently 0 :(
number += 1;
var d = new Dictionary<string, object>
{
["message"] = "Hello",
["number"] = number
};
return Ok(d);
}
let input = { number: 5825 }
$.ajax(
{
url: `/api/add`,
type: 'POST',
dataType: "json",
contentType: "application/json; charset=utf-8",
data: JSON.stringify(input),
accepts: "application/json",
complete: function (output) {
// completed
}
}
);
My service configurations are as follows:
services.AddSession();
services.AddHttpContextAccessor();
services.AddControllersWithViews();
And the middleware I use includes:
app.UseSession();
app.UseStaticFiles();
app.UseRouting();
app.UseCors();
app.UseAuthorization();
app.UseEndpoints(endpoints => { endpoints.MapControllerRoute("default", "{controller=Default}/{action=Index}/{id?}"); });