I have a class with property that i want use in json deserialization but i want omit in json serialization.
Example: I have a class user:
public class UserDTO
{
public string UserName { get; set; }
public string Password { get; set; }
}
and I use it in ASP.NET Web API.
I use this class for login and for get all users. Obviously, in get I want see only username, but in login I need username and password.
I have try to use attribute [IgnoreDataMember]:
public class UserDTO
{
public string UserName { get; set; }
[IgnoreDataMember]
public string Password { get; set; }
}
or decorate my class with DataContract
[DataContract]
public class UserDTO
{
[DataMember]
public string UserName { get; set; }
public string Password { get; set; }
}
but in this mode I can't login, indeed if I don't use attribute [DataContract] or [IgnoreDataMember] I can use login, but in get I see password.
I try also [NotSerialized] using backing field, like suggest here, but I obtain the same result.