Web Api Required Parameter

前端 未结 5 809
闹比i
闹比i 2020-12-14 06:10

Using ASP.NET Web API. Is there a way to automatically return a status code 400 if a parameter is null? I found this question but that is a global solution that is applied

5条回答
  •  轮回少年
    2020-12-14 06:39

    Set [Required] on a property in your model and then check the ModelState to see if it IsValid.

    This will allow all the required properties to be tested at the same time.

    See the "Under-Posting" section @ Model validation in WebAPI

提交回复
热议问题