0

I have a Model Class (entity framework) User, and when doing a Request.CreateResponse, I don't want its' password property to be serialized and sent.

I read in a SO post that there's a [JsonIgnore] for such cases, to be used in the property, but for some reason my Visual Studio doesn't know how to resolve it (I'm guessing it isn't from the web api library itself...? I'm pretty new to C#, but VS can't seem to suggest it using Ctrl+. )

So anyway, what is the best advice here? If possible I don't want to make a DTO just for that.

GBarroso
  • 467
  • 1
  • 8
  • 19

2 Answers2

0

You can use DataAnnotations on the property that you want to hide in serialization:

using Newtonsoft.Json

[JsonIgnore]
public string password {get; set;}

Use it on each property you want to hide.

0

Keep your model isolated from the way it will be serialized or deserialized.

If you have specific fields to be returned to your API consumers, then create a DTO that has only the data your method should return.

You can use AutoMapper in this case to map between your DTO and Entities classes

Haitham Shaddad
  • 4,336
  • 2
  • 14
  • 19