ModelState.IsValid even when it should not be?

前端 未结 10 1365
说谎
说谎 2020-11-29 03:14

I have API where I need to validate my user model. I choose an approach where I create different classes for Create/Edit actions to avoid mass-assignment and divide validati

10条回答
  •  野性不改
    2020-11-29 03:44

    What I did was to create an Attribute along with an ActionFilter and a Extension Method to avoid null models.

    The extension method looks for parameters with the NotNull attribute and check if they are null, if true, they are instantiated and set in the ActionArguments property.

    This solution can be found here: https://gist.github.com/arielmoraes/63a39a758026b47483c405b77c3e96b9

提交回复
热议问题