Search code examples
c#restasp.net-web-apihateoas

Adding HATEOAS links to response


I'm working on including hateoas links in the response. Specifically, for the kitIncludes field. I want to have a link to each record. For example, products/jason-component

Where would I have this link at? Would it be a field in the kitIncludes field? or should this be in the links under the knockout field?

{
"links": [
    {
        "rel": "self",
        "href": "http://localhost:60778/v2/products?page=0&limit=100"
    },
    {
        "rel": "first",
        "href": "http://localhost:60778/v2/products?page=0&limit=100"
    },
    {
        "rel": "last",
        "href": "http://localhost:60778/v2/products?page=0&limit=100"
    }
],
"metadata": {
    "totalRecords": 1
},
"entities": [
    {
        "additionalDimensions": null,
        "assemblyInstructionsUrl": null,           
        "detailedDescription": null,
        "dimensionSketch": null,
        "kitIncludes": [
            {
                "sku": "jason-component",
                "quantity": 12
            }
        ],
        "fobBasePrice": 0,
        "friendlyDescription": null,  
        "itemsPerCase": 1,
        "itemType": "Kit",
        "itemWeightKg": 0,
        "itemWeightLbs": 0,
        "knockout": null,            
        "links": [
            {
                "rel": "self",
                "href": "/products/JASON-KIT"
            },
            {
                "rel": "series",
                "href": "/series/"
            }
        ],
        "manufacturerWarrantyDays": 0,
        "mechanismGuideUrl": null,           
        "partsDrawingUrl": null,
        "productDetails": null,
        "productVideo": null,    
        "upc": null,
        "vendorName": "Test"
    }
]

}


Solution

  • Where would I have this link at?

    You might want to have a look at

    It could be less work, in the long run, to use common media type for your representations, rather than trying to roll your own.

    For the most part, they share this idea in common: if you have found some element in the representation, and you want to see if it has any links, the path you take to find out is always in the same place relative to the element itself.

    That suggests that you should expect to have a links collection for each kitIncludes entry.

    And that should make sense: how would you do it with a web page? Would you have a bunch of descriptions of kitIncludes in one place, and then somewhere else in the document a bunch of hyperlinks? It's much more likely that you would embed the links within the description of the item itself.