Good day.

Need your help. There is an ASP.NET WebApi application in which you need to submit a link between two resources. For example, there is a StreetType entity that returns json:

 { "id":3, "name":"StreetType1", } 

and the essence of Street :

 { "id":1, "name":"Street1", "streettypeid":3 } 

Previously, to obtain related resources, I used the OData request: http://localhost:3761/api/Street?$expand=StreetType

But, since in some entities there may be 3 or more related resources, then it will be difficult to write such requests, and not very beautiful.

It would be nice to implement this as follows:

  1. http://localhost:3761/api/Street/ - all Street
  2. http://localhost:3761/api/full/Street/ - all Street and related resources (in this case only StreetType)
  3. http://localhost:3761/api/Street/1 - Street with ID = 1
  4. http://localhost:3761/api/full/Street/1 - Street with ID = 1 and related resources

But there is a problem with the routing system and attribute inheritance. Since all this should be implemented in the base class.

Question: How to correctly implement the representation of related objects? How to issue a simple URL for example for such an OData request: http://localhost:3761/api/City?$expand=CityType,Region

    1 answer 1

    There is a small book on the design of API interfaces by Brian Mulloy, called Web API Design Crafting Interfaces that Developers Love. It gives examples of how your problem was solved by large corporations, in particular

     Facebook: /joe.smith/friends?fields=id,name,picture Google: ?fields=title,media:group(media:thumbnail) LinkedIn: /people:(id,first-name,last-name,industry) 

    It is also possible to divide the fields of the entity into mandatory and additional, where mandatory always to unload, and additional fields upon request.

    In general, the request http: // localhost: 3761 / api / City ? $ Expand = CityType, Region looks wrong, because you link different entities. It is more logical to receive the Region as a separate request.