2

I created a WebApi project with EF and when configured it for json response, it started giving me a json object of query (entity) with all the data in navigation properties. i switched off the lazy loading from EF and now i can see that my response does not contain any data from navigation entities. But still , there are few empty arrays been shown in response. how can i completly get rid of them?

beside that, json response is adding $.id attribute to my response, can we remove it as well?

{
$id: "1",
ChargePoints: [ ],
ChargerActionHistory: [ ],
ChargingBoxModels: null,
ChargingStations: null,
StatusCodes: null,
WallSettings: null,
Heartbeats: [ ],
PK_ChargingBoxID: 2,
FK_ChargingStationsID: 1,
FK_WallSettingsID: 1,
FK_StatusCodeID: 1,
FK_ChargingBoxModelID: 1,
DeviceID: "HUB399209-UK",
ChargingBoxName: "ArneCharger",
CSEndpoint: null,
CBEndpoint: null,
CSPort: null,
CBPort: null,
DeviceIP: null,
OperationalPhase: null,
HeartbeatInterval: 5,
MeterInterval: 10,
Notes: "test device",
CommissionedDate: "2016-05-23T13:52:07.193",
ExpiryDate: "2016-06-23T13:52:07.193",
isAuthenticated: true,
isPublic: true,
isActive: true,
Keys: "39c5f761-5c08-4b3d-9b88-adcfd75ss26b"
}
3
  • some code snippet gives more meaning to your problem. Commented Jul 28, 2016 at 15:38
  • 1
    Usual you don't return the domain models directly. You need to convert them to POCO objects and return them to public. While creating the POCO you can specify what properties you want to expose to the world. Commented Jul 28, 2016 at 15:39
  • isn't this conversion an overhead? Like convertion domain models to specific poco objects and to user and then from poco to domain model. Commented Aug 8, 2016 at 17:00

1 Answer 1

2

This can be done using Serialization Attributes.

The ASP.NET documentation is a good start into how to do this. http://www.asp.net/web-api/overview/formats-and-model-binding/json-and-xml-serialization

Generally you should avoid exposing your entities and instead map to a ViewModel POCO. However in an a simple application you can get away with doing this.

Sign up to request clarification or add additional context in comments.

1 Comment

Can you guide me toward how ViewModel POCO are implemented. I did a database first approach and then generated code from model using EF 6. isn't that Code is POCO based classes? I want to implement my API using S.O.L.I.D . Sorry if I am being naive,

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.