/// <summary>
        /// Gets a customer with HAL formatted hypermedia.
        /// </summary>
        public HttpResponseMessage Get(HttpRequestMessage request) {

            //this will be our embedded resource.
            var customer = new Customer
                {
                    Name = "Joe Blow",
                    Title = "President"
                };

            //this will be our root resource
            var orderLog = new OrderLog
                {
                    NumberOfItemsPurchase = 2,
                    OrderDate = new DateTime(1999, 12, 31),
                    TotalAmount = 43.23m
                };

            //Create a builder so we can build our HAL document that will get serialized.
            var bldr = new HalDocumentBuilder(orderLog);

            //Add all of our links to our HAL document.
            //Note: The use of relative paths for brevity.
            bldr.IncludeRelationWithSingleLink(HalRelation.CreateSelfRelation(), new HalLink("/orderlogs/123"));
            bldr.IncludeRelationWithMultipleLinks(new HalRelation("archive"),
                                                  new[]
                                                      {
                                                          new HalLink("/orderlogs/archive/") { Profile = "http://profiles.mydomain.com/standard/"},
                                                          new HalLink("m/orderlogs/archive") { Profile = "http://profiles.mydomain.com/mobile/"}
                                                      });

            //Create a builder to build our Customer embedded resource.
            var embeddedBldr =
                new HalEmbeddedResourceBuilder(customer);

            //Add all of our HAL links that will be on the Customer embedded resource.
            embeddedBldr
                .IncludeRelationWithSingleLink( HalRelation.CreateSelfRelation(),
                                               new HalLink("/customer/112")
                                                   {
                                                       Profile =
                                                           "https://profiles.mydomain.com/customer/"
                                                   })
                .IncludeRelationWithMultipleLinks(new HalRelation("tags"),
                                                  new[] {new HalLink("/tags/123"), new HalLink("tags/345")});

            //Create an embedded resource with the links we added through the builder.
            var embeddedResource = new HalEmbeddedResource(embeddedBldr);

            //Add our single embedded resource to our HAL document builder.
            bldr.IncludeEmbeddedWithSingleResource(new HalRelation("customer"), embeddedResource);


            var embeds = new List<HalEmbeddedResource>
                {
                    new HalEmbeddedResource(embeddedBldr),
                    new HalEmbeddedResource(embeddedBldr)
                };

            //Add a relation that has 2 embedded resources.
            bldr.IncludeEmbeddedWithMultipleResources(new HalRelation("contrived"),
                                                      embeds);

            //Build our HAL document.
            HalDocument document = bldr.Build();

            //Create a response that will have our HAL document. We use the appropriate media type on the response.
            //The HalDocument we built will get serialized into HAL formatted JSON since we added the HalJsonMediaTypeFormatter
            //to the application's formatters collection during application start up in the WebApiConfig.cs file.
            var response = request.CreateResponse(HttpStatusCode.OK, document,
                                                  HalJsonMediaTypeFormatter.SupportedMediaType);

            return response;
        }
 /// <summary>
 /// Adds a single embedded relation with a single resource to this document.
 /// </summary>
 /// <example>
 /// Sample JSON output:
 /// {
 ///    "_links": { "self": { "href": "http://mysite.com/" } },
 ///    _embedded: {
 ///               "order": {
 ///                           "_links": { "self": { "href": "http://mysite.com/{id}/", "templated": true },
 ///                           "orderDate": "2013-01-05T13:43:22.800000Z",
 ///                           "items": [ ... ]                        
 ///                       }
 ///               } 
 /// }
 /// </example>
 /// <param name="relation">How the embedded resource is related to the root resource.</param>
 /// <param name="embeddedResource">The embedded resource.</param>
 /// <returns>This <see cref="IHalDocumentBuilder"/> instance.</returns>
 public IHalDocumentBuilder IncludeEmbeddedWithSingleResource ( HalRelation relation, HalEmbeddedResource embeddedResource )
 {
     if (relation == null)
     {
         throw new ArgumentNullException("relation");
     }
     if (embeddedResource == null)
     {
         throw new ArgumentNullException("embeddedResource");
     }
     _embeddedResourceCollection.Add(relation, embeddedResource);
     return this;
 }
Beispiel #3
0
 /// <summary>
 /// Adds a single embedded relation with a single resource to this document.
 /// </summary>
 /// <example>
 /// Sample JSON output:
 /// {
 ///    "_links": { "self": { "href": "http://mysite.com/" } },
 ///    _embedded: {
 ///               "order": {
 ///                           "_links": { "self": { "href": "http://mysite.com/{id}/", "templated": true },
 ///                           "orderDate": "2013-01-05T13:43:22.800000Z",
 ///                           "items": [ ... ]
 ///                       }
 ///               }
 /// }
 /// </example>
 /// <param name="relation">How the embedded resource is related to the root resource.</param>
 /// <param name="embeddedResource">The embedded resource.</param>
 /// <returns>This <see cref="IHalDocumentBuilder"/> instance.</returns>
 public IHalDocumentBuilder IncludeEmbeddedWithSingleResource(HalRelation relation, HalEmbeddedResource embeddedResource)
 {
     if (relation == null)
     {
         throw new ArgumentNullException("relation");
     }
     if (embeddedResource == null)
     {
         throw new ArgumentNullException("embeddedResource");
     }
     _embeddedResourceCollection.Add(relation, embeddedResource);
     return(this);
 }