diff --git a/aspnetcore/web-api/define-controller/samples/WebApiSample.Api.Pre21/Controllers/PetsController.cs b/aspnetcore/web-api/define-controller/samples/WebApiSample.Api.Pre21/Controllers/PetsController.cs index 93127dec43..22de01e058 100644 --- a/aspnetcore/web-api/define-controller/samples/WebApiSample.Api.Pre21/Controllers/PetsController.cs +++ b/aspnetcore/web-api/define-controller/samples/WebApiSample.Api.Pre21/Controllers/PetsController.cs @@ -47,10 +47,12 @@ namespace WebApiSample.Api.Controllers [ProducesResponseType(400)] public async Task CreateAsync([FromBody] Pet pet) { + #region snippet_ModelStateIsValidCheck if (!ModelState.IsValid) { return BadRequest(ModelState); } + #endregion await _repository.AddPetAsync(pet); diff --git a/aspnetcore/web-api/index.md b/aspnetcore/web-api/index.md index 140fa6b169..d2296c5da8 100644 --- a/aspnetcore/web-api/index.md +++ b/aspnetcore/web-api/index.md @@ -57,7 +57,7 @@ The following sections describe convenience features added by the attribute. Validation errors automatically trigger an HTTP 400 response. The following code becomes unnecessary in your actions: -[!code-csharp[](../web-api/define-controller/samples/WebApiSample.Api.Pre21/Controllers/PetsController.cs?range=46-49)] +[!code-csharp[](../web-api/define-controller/samples/WebApiSample.Api.Pre21/Controllers/PetsController.cs?name=snippet_ModelStateIsValidCheck)] The default behavior is disabled when the [SuppressModelStateInvalidFilter](/dotnet/api/microsoft.aspnetcore.mvc.apibehavioroptions.suppressmodelstateinvalidfilter) property is set to `true`. Add the following code in *Startup.ConfigureServices* after `services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);`: