Where should I add [JsonIgnore] to prevent certain properties from being serialized?

前端 未结 2 1456
渐次进展
渐次进展 2021-01-13 00:28

This is a very simple Web API project. I have a data model, generated DbContext, and a controller.

When I add the [JsonIgnore] attribute to certain prop

2条回答
  •  渐次进展
    2021-01-13 00:44

    You should use view models. Basically define classes that will contain only the properties that you need to expose and then return those view models from your Web API actions. This way you don't have to worry about polluting your domain models with [JsonIgnore] attributes especially if you don't want those properties to be ignored only for certain actions. In order to simplify the mapping between your domain models and view models you may take a look at AutoMapper.

提交回复
热议问题