Validation datacontract and datamember required

后端 未结 1 354
遇见更好的自我
遇见更好的自我 2021-01-19 10:49

I\'m using the web api to build an API and when receiving the posted values and binding them to my model I get an error that seems out of place.

I have a simple mod

相关标签:
1条回答
  • 2021-01-19 11:30

    The reason this validation is there is because for reference-typed members, whenever the member is deserialized WebAPI can check that the member is not null. For value types, there is no null value so it's up to the formatter to check that the value is present in the request body. Unfortunately, our XML formatter doesn't support the [Required] attribute so it won't raise a model state error if the member is missing.

    If you're OK with certain formatters not raising model state errors for the missing value-typed members, you can use this line to remove the validation:

    config.Services.RemoveAll(typeof(ModelValidatorProvider), (provider) => provider is InvalidModelValidatorProvider);
    
    0 讨论(0)
提交回复
热议问题