public IHttpActionResult GetBar(int fooId) { // A collection of bars related to foo var bars = new List<object> { new { id = 1, fooId = fooId, type = "bar" }, new { id = 2, fooId = fooId, type = "bar" } }; // data about the bars related to foo var fooBarModel = new { fooId = fooId, count = bars.Count }; // Return a fooBar resource with embedded bars return this.HAL( fooBarModel, new Link[] { new Link("self", "/api/foo/{fooId}/bar") }, "bars", bars, new Link[] { new Link("self", "/api/bar/{id}") } ); }
public IHttpActionResult Get(int id) { // Any plain old object will do var fooModel = new { id = id, type = "foo" }; // Return a simple resource with links to related resources return this.HAL(fooModel, new Link[] { new Link("self", "/api/foo/{id}"), new Link("foo:bar", "/api/foo/{id}/bar") }); }
private async Task AssertModelJson(object model, string contentType, bool forceHal, string expected) { using (var stream = new MemoryStream()) { var content = new StringContent(""); content.Headers.ContentType = new MediaTypeHeaderValue(contentType); var formatter = new JsonHALMediaTypeFormatter(); var link = new Link("self", "href"); var embedded = new[] { new { bar = true } }; var halModel = new HALResponse(model, new HALModelConfig { ForceHAL = forceHal }) .AddLinks(link) .AddEmbeddedCollection("bars", embedded); Assert.NotNull(formatter); await formatter.WriteToStreamAsync(typeof(HALResponse), halModel, stream, content, null); // Reset the position to ensure it can read stream.Position = 0; var reader = new StreamReader(stream); string result = await reader.ReadToEndAsync(); Assert.Equal(expected, result); } }
public async Task Write_To_Stream_Test(string contentType, bool forceHal, string expected) { var model = new { foo = 1 }; await AssertModelJson(model, contentType, forceHal, expected); }