5

I call Register method with empty username and password. So I received this result:

{
    "errors": {
        "Password": [
            "The Password field is required.",
            "Password length is between 4 and 8."
        ],
        "Username": [
            "The Username field is required."
        ]
    },
    "title": "One or more validation errors occurred.",
    "status": 400,
    "traceId": "0HLJIO56EGJEV:00000001"
}

My Dto:

public class UserForRegisterDto
{
    [Required]
    public string Username { get; set; }
    [Required]
    [StringLength(8, MinimumLength = 4, ErrorMessage = "Password length is between 4 and 8.")]
    public string Password { get; set; }
}

I only want to get errors attribute from response, What should I do?

1
  • Where / how are you validating the story? Could you update your question to show this. I have used ModelState.IsValid to check the model state and if it's not valid you could extract only the errors and return in a response. Commented Jan 5, 2019 at 9:53

2 Answers 2

5

This is a new feature in ASP.NET Core 2.2:

An IActionResult returning a client error status code (4xx) now returns a ProblemDetails body.

The docs describe that this can be disabled when calling AddMvc inside of ConfigureServices, like this:

services.AddMvc()
    .SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
    .ConfigureApiBehaviorOptions(options =>
    {
        options.SuppressUseValidationProblemDetailsForInvalidModelStateResponses = true;
    });

This will result in the pre-2.2 behavior, which will serialise only the errors.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, I fixed it. I added this code in ConfigureApiBehaviorOptions: options.SuppressModelStateInvalidFilter = true; it worked!
Did you try my solution? What you've gone with is fixing automatic validation rather than specifically avoiding the more verbose response it generates.
Yes! I tried your solution. But the response that I received like the beginning.
0

As I went through a very close scenario, I will add here an answer just in case others run into the same or similar issues:

In my case, the problem involved a project with .NET Core 3.1 as Target Framework. My project had a NuGet package dll as one of its dependencies and such package had several models. Our controller endpoints had these models as parameters. However, requests sent to any of these endpoints (with any of those models as body) were giving rise to validation errors for non-nullable reference type properties.

The workaround to solve it was similar to the one suggested by Kirk. I have added the following option to AddControllers method, from Startup's ConfigureServices method:

services.AddControllers(options => options.SuppressImplicitRequiredAttributeForNonNullableReferenceTypes = true);

Note that, by using this workaround, it will be necessary to manually add [Required] attributes for nullable reference type properties around your application.

I also opened an issue on their repository: https://github.com/dotnet/aspnetcore/issues/27448

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.