'Blazor server with ASP.NET Core MVC controller, validation issue
I am working on a project that is made with Blazor Server in .NET 6. I have added controllers for the authentication flow signup, registering and connecting external signin providers.
When wanting to send a GET request to a endpoint I made with the route /register the page loads as expected. On the other hand when a POST is send and the validation is not working, the response looks like this.
{
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
"title": "One or more validation errors occurred.",
"status": 400,
"traceId": "00-5f48e143e34dbb3a8d4223b7a912d679-48179933e0c1adc8-00",
"errors": {
"Email": [
"The Email field is required."
],
"Phone": [
"The Phone field is required."
],
"Lastname": [
"The Lastname field is required."
],
"Password": [
"The Password field is required."
],
"Firstname": [
"The Firstname field is required."
],
"CountryCode": [
"The CountryCode field is required."
]
}
}
No matter what I do and how I do it, the validation will be returned as json, even if the fields that are filled in and meets the requirements. The json is returned with the missing fields, which is fine.
The thing is I can verify the validation is working, since the missing fields is shrinking as fields gets the correct user input.
The expected behaviour is to show the errors above the fields in the register form for instance.
Solution 1:[1]
The solution has been found, after some researching i found out that my /register endpoint were handled like an API endpoint, which it wasn't.
To fix this issue, this code had to bee put in the Program.cs file
builder.Services.Configure<ApiBehaviorOptions>(options =>
{
options.SuppressModelStateInvalidFilter = true;
});
Beaware, this disables pre request model validation for all of your controllers, now the controllers and views are on their own.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Kristian B. Kuhrt |
