can not use asp-page-handler in asp.net core web?

mc 7,276 Reputation points
2026-08-22T04:53:09.93+00:00

I am using asp.net core web 10.0 and I want to use handler and I get 400 Bad Request.

    public JsonResult OnPostOrder(GoodsO[] g)
    {
        return new JsonResult(new { Status = true });
    }
public class GoodsO
{
    [JsonPropertyName("o")]
    public int O { get; set; }
    [JsonPropertyName("o1")]
    public int O1 { get; set; }
    [JsonPropertyName("value")]
    public int Value1 { get; set; }
    [JsonPropertyName("value1")]
    public int Value { get; set; }
}

and this is what I post

User's image

http://localhost:5179/index?handler=Order&__RequestVerificationToken=CfDJ8DUdemiDYhxOuf0WiqNgJ89XL3yF2UZeMMLVRmNjKPLgtSPqPDYTQBehRiI_BOWPOC6_Cs96Yhz7fhZDWRMfQJmOFdHiPjJe_2z9FeqxkqKxIdJ1DZYwGl89d6fcYi0-p61AyevcNdItGF4vCDFnngVjvh6HvqDTcnwxFKTlXUaD-78kBMopX8jdedJ5b3mr3g

Developer technologies | ASP.NET Core | ASP.NET prefix
0 comments No comments

Answer accepted by question author
AgaveJoe 31,546 Reputation points
2026-08-22T13:33:24.8633333+00:00

A 400 Bad Request is expected here because of how the anti-forgery token is being passed.

By default, ASP.NET Core's antiforgery middleware rejects or ignores tokens in the query string. Tokens must be sent either inside the form body or via an HTTP header (such as RequestVerificationToken). Passing tokens in the URL is intentionally not supported out of the box because query strings are exposed in browser history, proxy logs, and server access logs.

Additionally, because your model uses [JsonPropertyName], you are likely sending a JSON payload via JavaScript (fetch or axios). In Razor Pages, you must add the [FromBody] attribute to the handler parameter to bind JSON:

public JsonResult OnPostOrder([FromBody] GoodsO[] g)
{
    return new JsonResult(new { Status = true });
}

To give you an exact fix, could you share the JavaScript/client-side code you are using to send the request?

If you are using fetch, you typically need to send the token in the request headers instead:

fetch('?handler=Order', {
    method: 'POST',
    headers: {
        'Content-Type': 'application/json',
        'RequestVerificationToken': document.querySelector('input[name="__RequestVerificationToken"]').value
    },
    body: JSON.stringify(payload)
});

Was this answer helpful?

1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.