3

I'm working with entities in WebApi application that have a linked entities.
For example:

  • Entity Street has the following related resource: City
  • Entity City has: Region, CityType
  • Entity Person: Address, Nationality, Position, Catalog, Documents ...

How can I proper route related entities?

Before I used OData request like: http://localhost:3761/api/City?$expand=CityType,Region for City and http://localhost:3761/api/Person?$expand=Address,Nationality,Documents,Catalog for Person.
But most client do not know which exist related objects. And they want to add some parameter to request that will get entity with all linked data.
Example http://localhost:3761/api/Person/full or http://localhost:3761/api/Person/2/full for Person with ID = 2.

I tried to implement it but I have problem with routing. So as I have a base api controller then attributes is not inherited to my child controller and as a result it doesn't work.

Could you please suggest me what is the best practice to route related entities?

P.S: Sorry for my English and let me know if something is not clear.
Thanks

Community
  • 1
  • 1

1 Answers1

0

Look at how Trello has organized their API Trello API

They allow one level of pivoting and the posibility to declare what kind of data your are interested in. There have default fields which you get returned unless you specify a list of fields from a valid list.

Example: GET: api/1/cards/card id/members (Options)

In your case this would transform into something like this:

http://localhost:3761/api/people/2?&fields=address,nationality,position,catalog

Jakob Pilsgaard
  • 120
  • 2
  • 7
  • Thanks but I know hot to return fields. I ask about related entities (object in object). I found similar problem http://stackoverflow.com/questions/32620089/asp-net-webapi-creating-one-level-json. But there is not answer that help me –  Sep 21 '15 at 12:31
  • In my case `api/Person?$expand=Address,Nationality,Documents,Catalog` IS NOT a fields this is related entities (using navigation property as Foreign Keys) –  Sep 21 '15 at 12:34