Home > Net >  Unable to call onPost in Razor Pages
Unable to call onPost in Razor Pages

Time:12-07

I want to send JSON data to a razor page using POST method: https://localhost:port/Post and get a JSON output.

For example:

I want to pass username and email as JSON:

{"username":"test","email":"[email protected]"}

to onPost method of Post.cshtml page: https://localhost:44363/Post:

public JsonResult OnPost([FromForm] Dictionary<String, String> FormValues)
{
    String username = FormValues["username"];
    String email = FormValues["email"];

    Dictionary<String,String> dict = new Dictionary<String, String>();

    dict.Add("username", username);
    dict.Add("email", email);

    return new JsonResult(dict);
}

I am calling this POST method using HttpClient in WinForms as:

HttpClient client = new HttpClient();

Dictionary<String, String> dict1 = new Dictionary<String, String>();

dict1.Add("username", "test");
dict1.Add("email", "[email protected]");

String JSON = JsonConvert.SerializeObject(dict1);

HttpResponseMessage Result;

Result = client.PostAsync("https://localhost:44363/Post", new StringContent(JSON, Encoding.UTF8, "application/json")).Result;

String json = Result.Content.ReadAsStringAsync().Result;

Dictionary<String, String> dict2 = JsonConvert.DeserializeObject<Dictionary<String, String>>(json);

But I am getting bad request 400 error.

I have also tried FromBody as: public JsonResult OnPost([FromBody] Dictionary<String, String> FormValues) but onPost does not execute.

However, OnGet method is working fine:

public JsonResult OnGet()
{
    Dictionary<String, String> dict = new Dictionary<String, String>();

    dict.Add("username", "test");
    dict.Add("email", "[email protected]");

    return new JsonResult(dict);
}

For HttpClient:

HttpClient client = new HttpClient();

HttpResponseMessage Result;

Result = client.GetAsync("https://localhost:44363/Post").Result;

String json = Result.Content.ReadAsStringAsync().Result;

Dictionary<String, String> dict2 = JsonConvert.DeserializeObject<Dictionary<String, String>>(json);

CodePudding user response:

POST handlers in Razor Pages have request verification enabled by default. This is to prevent cross site request forgery. Usually, the advice is to include the verification token within the request, but in your case, the simplest solution would be to disable the check for the token at PageModel level:

[IgnoreAntiforgeryToken(Order = 1001)]
public class PostModel : PageModel
{
   ...

Incidentally, if you are posting JSON, you need to use the FromBody attribute on your handler parameter, not FromForm.

  • Related