Asp.net Core 2.0 Razor Pages Ajax Post Asp.net Core 2.0 Razor Pages Ajax Post ajax ajax

Asp.net Core 2.0 Razor Pages Ajax Post


Your URL formation for Ajax request is correct. One thing to note down is, Razor Pages are designed to be protected from (CSRF/XSRF) attacks. Hence, Antiforgery token generation and validation are automatically included in Razor Pages. I believe that is the problem here. Your page may have antiforgery token present on the page if you have form tag in your HTML. But you need to pass the same in your Ajax request.

First, add antiforgery token using @Html.AntiForgeryToken(), if not present.

Then, modify your Ajax request to send the same in request header.

Like,

beforeSend: function (xhr) {       xhr.setRequestHeader("XSRF-TOKEN",          $('input:hidden[name="__RequestVerificationToken"]').val());},