Merge pull request #7802 from aspnet/scottaddie/model-state-patch

Replace line numbers range with snippet name
pull/7803/head
Scott Addie 2018-07-24 21:52:46 -05:00 committed by GitHub
commit 95407f8f26
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 3 additions and 1 deletions

View File

@ -47,10 +47,12 @@ namespace WebApiSample.Api.Controllers
[ProducesResponseType(400)]
public async Task<IActionResult> CreateAsync([FromBody] Pet pet)
{
#region snippet_ModelStateIsValidCheck
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
#endregion
await _repository.AddPetAsync(pet);

View File

@ -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);`: