0

this is related to a post I made yesterday, but havent been able to resolve: ASP.Net Web API showing correctly in VS but giving HTTP500

I think I need to simplify what I'm trying to do, and work up from there.

Can anyone please point me to an example of using the asp.net Web API (I'm using VS 2012 Express RC), to return JSON from a parent/child model?

eg: (pseudo Json):

Parent: Mark
..Child: Tom
..Child: Adam
..Child: Becki

Parent: Terry
..Child: Sophie
..Child: robert

I can get it to return data from one table, but not from a linked table.

Thanks for any help,

Mark

Community
  • 1
  • 1
Mark
  • 7,778
  • 24
  • 89
  • 147

2 Answers2

1

There are two ways to go away with this.

  1. Create a new template class, loop through the list fetched using the EF and assign the values to the properties defined in the template class. This would give you the accurate results from one to multiple tables if any. Finally return the list to the json call.

  2. While fetching the list from the EF, create a new anonymous type and select your desired columns. For this your webmethod would have the return type as IEnumerable

Cheers!

Umar Malik
  • 63
  • 4
  • 10
  • Hi @Umar-Malik - thank you - I have tried several methods (in the previous post I mentioned above), but even with a lot of suggestions, have not been able to get this (on the surface, simple) return from the web api. I'm hoping to see a very simple example, which I can then build on - but can't get past the starting line! – Mark Jun 06 '12 at 12:07
  • Hi @fixit sample code for ur help is as under:`[WebMethod] public static IEnumerable loadAllNotes(string title, string body){ MyFacade cf = new MyFacade(GetProfileCommonObject().Personal.UserName, GetProfileCommonObject().Personal.tenantID, GetProfileCommonObject().Personal.UserID); List list = cf.loadAllNotes(title, body); IEnumerable newList = null; if (list.Count > 0) { newList = list.Select(x => new { x.Body, x.Title, x.CreatedDate, x.MyNoteID }); } return newList; }` – Umar Malik Jul 25 '12 at 08:51
0

After looking at your original post my guess is that you have circular references in your objects. This post makes reference to using Json.Net which will give you more control over what is being returned to the client.

Your other option is to remove the foreign key reference tblCustomerBooking from the tblRental object (see below).

This may allow you to return the JSON objects and test that circular references are the issue.

[ForeignKey("customer_id")]
public virtual tblCustomerBooking tblCustomerBooking { get; set; }

I do suggest using Json.NET if you're planning on returning your Domain (i.e. Entity Objects) as this will avoid all circular references, and allow you keep your two-way object relationships.

My personal preference is to using DTO's and map your Domain objects to these DTO's, allowing you to have more control over what the client sees (as seeing the 'tbl' prefix in a object name isn't good practice)

Community
  • 1
  • 1
leon.io
  • 2,779
  • 1
  • 18
  • 26
  • Hi @Leon - I'm using VS 2012 RC, which I understand uses Json.Net by default now. Do you mean take out both of these lines from the model: [ForeignKey("customer_id")] public virtual tblCustomerBooking tblCustomerBooking { get; set; } - should I also remove the FK on the tblRental table in the database? (thank you for your help) – Mark Jun 06 '12 at 16:38
  • Hi @Leon - thank you - it does indeed appear to be the "circular reference" which appears to be caused by having the two lines you noted above. Although I'm not 100% clear on why it's recommended to have those lines in many other posts, or what the consequences are yet, of removing them. But for now, I'm getting my parent/child relationship sent back without error, in JSON format. Thanks to you, and everyone else who has helped me this week - I really appreciate the efforts of so many people, including filip-w mark and others. – Mark Jun 07 '12 at 08:29
  • Hi Mark - your welcome, glad it helped - to answer your question about which property to remove, it's on the `tblCustomerBooking.customer_id` property. You can also keep that property, but use the `IgnoreDataMember` so it is not serialized. [Check out this related post](http://stackoverflow.com/questions/10038628/error-when-serializing-ef-code-first-5-0-data-in-webapi-controller) - looks like a few people are running into this same "gotcha" with VS2012 RC. – leon.io Jun 07 '12 at 10:04