Created by @smarts in #10191
The model validation docs do not state how to enable validation for Controller Action parameters. Other than an official announcement blog post that briefly mentions the feature, this was the best example that I could find. When I tried that and it didn't work I looked around Stack Overflow and found this answer, which led me to poke around and ultimately find this setting:
c#
public void ConfigureServices(IServiceCollection services)
{
…
services.AddMvc(x => x.AllowValidatingTopLevelNodes = true);
}
It'd be great if an example of top-level, primitive type model validation was added to the docs. Thanks!
⚠Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
@guardrex can you take this? Make sure I review it. The links are pretty good. I'd like to see RP code and Web API code.
Yes ... let me get past a little batch of fairly quick P1 issues. I'll get it done this sprint.
@Rick-Anderson I'm going to take the time to add this to the sample app, too. It will take a bit longer, but I think it will compose well with the other examples. If you prefer that I _NOT_ do that, plz let me know quickly. I'm working this one right now.
Samples are always a major bonus. For many things, like this, required.
Thanks for adding this @Rick-Anderson , but the docs now incorrectly state that MvcOptions.AllowValidatingTopLevelNodes is true by default and can be disabled, but that's only true if the compatibility version is set to CompatibilityVersion.Version_2_1, which does _not_ match the example. If the compatibility version is CompatibilityVersion.Version_2_2 (as in the example), then the property must be explicitly set to true.
I'll take a look @smarts.
@smarts I'm going to move this to a new issue. We don't work closed ones.