aspnet / Mvc

[Archived] ASP.NET Core MVC is a model view controller framework for building dynamic web sites with clean separation of concerns, including the merged MVC, Web API, and Web Pages w/ Razor. Project moved to https://github.com/aspnet/AspNetCore
Apache License 2.0
5.62k stars 2.14k forks source link

A problem about model binding in webapi #8607

Closed Eilon closed 6 years ago

Eilon commented 6 years ago

From @fanslead on Friday, 12 October 2018 07:31:38

Environment: .net core sdk version 2.1.403 and Microsoft.AspNetCore.App version 2.1.5 A model like this image You have an api need this model parameter from body and Content-Type is application/json. Normally we will send the json data like image But if we make a mistake about the type of one of the fields, the model parameter we received in back-end will be null and without throw any exception. image I think even if no exception is thrown, the model parameter should not be null, keep the data of correct field at least.

Copied from original issue: aspnet/AspNetCore#3618

mkArtakMSFT commented 6 years ago

Thanks for contacting us, @fanslead.

Model binding validation doesn't throw automatically for non-api controllers. If you would like to get that behavior, you can decorate your controller with [ApiController] attribute.

@dougbu, can you please advice whether the behavior of setting the model to null even when parts of it have failed to validate is the expected behavior or not? Thanks!

fanslead commented 6 years ago

Thank you for your reply. I decorate my controller with [ApiController] attribute can get an exception info correctly.