I have the following Razor page setup:
[BindProperties]
public class MyFormPageModel : PageModel
{
public int Id { get; set; }
public void OnGet(){}
public IActionResult OnPost() {
return RedirectToPage("Index", new {Id});
}
}
And:
@page "{Id:int?}"
<form method="post">
<button type=submit>Submit</button>
</form>
If I visit /MyForm/1
The form+button loads up.
Clicking the button successfully posts to /MyForm/1
But this just produces a 400 error from the back end.
No errors show up in the output log of the backend either.
Found my issue.
The Anti Forgery Token was not being generated in this case, as the Form was being generated dynamically.
Razor automatically generates the Anti Forgery Token if you have a <form>
in the Razor .cshtml from the start, but as soon as your form is generated dynamically that does not work.
This page here provided some excellent solutions to my issue: https://exceptionnotfound.net/using-anti-forgery-tokens-in-asp-net-core-razor-pages/