Aleksander Aleksic
Aleksander Aleksic

Reputation: 1370

Exclude object property on JSON output

I am trying to create a user model with username and password. I dont want the password to be displayed in the api response, but it is required on signup. So [JsonIgnore] wont work. Any suggestions?

public class User {
    [Required]
    public string Username { get; set; }

    [Required]
    public string Password { get; set; }
}

Upvotes: 0

Views: 81

Answers (1)

Rahul
Rahul

Reputation: 77876

That's why you should separate your actual entity and model. You should create a model / viewmodel and send that as response from your API / MVC controller like

public class UserModel 
{
  public string Username { get; set; }
}

Another piece of advice, never include any data validation in your actual entity, rather you should have those validation [Required] in your model / viewmodel; and should keep the entity away from this clutter.

Upvotes: 3

Related Questions