Skip to content

Better Error Message in ControllerBase.ValidationProblem #15166

@bradlis7

Description

@bradlis7

Describe the bug

During Unit Tests, I was getting a NullReferenceExeption in ControllerBase.ValidationProblem. Difficult to narrow down the problem.

To Reproduce

Steps to reproduce the behavior:

  1. 3.0.0
  2. Create a controller unit test of an action that calls ValidationResult.
var c  = new MyController();
c.MyAction(); // Should call return new ValidationResult;
  1. NullReferenceException was due to ProblemDetailsFactory being null, as ProblemDetailsFactory => HttpContext?.RequestServices?.GetRequiredService<ProblemDetailsFactory>() silently returns null.

Expected behavior

Error message should be actual problem. NullReferenceException is not clear. Perhaps check for null when returning ProblemDetailsFactory property.

Additional context

Add any other context about the problem here.
Include the output of dotnet --info

Metadata

Metadata

Assignees

No one assigned

    Labels

    DoneThis issue has been fixedarea-mvcIncludes: MVC, Actions and Controllers, Localization, CORS, most templateshelp wantedUp for grabs. We would accept a PR to help resolve this issuetask

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions