0

I have an entity User in WebApi project,

public class User
{ 
  public string UserId { get; set; }
  [Required]
  public string FirstName { get; set; }
  [Required]
  public string LastName { get; set; }
}

Same entity has been used for POST and PATCH actions. FirstName and LastName fields are mandatory for POST method but the same is not mandatory for PATCH action.

So, could not use the same entity for these actions, is there any other option to use same entity instead of creating new entity for Patch action?

Thanks in advance!

Magendran V
  • 1,411
  • 3
  • 19
  • 33
  • Maybe it is possible to use inheritance? You can always create some `BaseUser` entity with only `UserId` and then create a child class `UserPOST` with those mandatory fields? You could use then this base class for your `PATCH` action :-) – Paradowski Jun 20 '18 at 06:45
  • You can implement `RequiredIf` Data annotation attribute; something like [this](https://stackoverflow.com/questions/20642328/how-to-put-conditional-required-attribute-into-class-property-to-work-with-web-a). So in case of Patch, those will not non mandatory and for Post, the fields would be mandatory. – user1672994 Jun 20 '18 at 06:48
  • https://stackoverflow.com/questions/31001927/different-models-for-restful-get-and-post, https://stackoverflow.com/questions/41427075/webapi-different-dto-for-get-and-post, – CodeCaster Jun 20 '18 at 07:00

0 Answers0