A HAL implementation for ASP.NET.
- Function-chaining paradigm when manually creating HAL models.
- Optional attribute-based construction of HAL models.
HalModel
: The base model.SetRelativeApiPath
: Sets the relative path for links.AddLinks(...)
: Adds a link to "_links" node of the HAL document.AddEmbeddedCollection(...)
: Adds a collection of values to the "_embeds" node of the HAL document.ToActionResult(...)
: (optional) Translates the HalModel into anIHttpActionResult
.
HalModel(..)
: This attribute contains basic settings for the model, such as the relative path for links.HalIgnore()
: This attribute causes the field to be ignored when creating the HAL document.HalLink(...)
: This attribute tell the field to display as a link in the HAL document instead of a field.HalReferenceObjects(...)
: This attribute allows embedded data to be represented as links or, using the "expand" query string, display the data embedded into the HAL document. eg. /attribution/23?expand=subAttributions
public class SimpleDto
{
public int Id { get; set; }
public string Comment { get; set; }
}
public class controller : ApiController
{
[HttpGet, Route("simpleton/{Id:int}")]
public IHttpActionResult GetSimpleton(int Id)
{
SimpleDto dto = ...;
return new HalModel(dto)
.SetRelativeApiPath("~/api/")
.AddLinks(...)
.AddEmbeddedCollection(...)
.ToActionResult(this);
}
}
//
// Here's what the models look like...
//
[HalModel("~/api/")]
public class AttributedDto
{
[HalLink(HalLink.RelForSelf, "attributions/{value}")]
public int Id { get; set; }
[HalLink("simpletons", "simpletons/{value}")]
public int SimpletonId { get; set; }
[HalReferenceObjects()]
public IEnumerable<SubAttributionDto> SubAttributions { get; set; }
}
[HalModel("~/api/")]
public class SubAttributionDto
{
[HalLink(HalLink.RelForSelf, "attributions/{value}")]
public int Id { get; set; }
}
//
// And here's what the API Controller looks like...
//
public class controller : ApiController
{
[HttpGet, Route("attribution/{Id:int}")]
public dynamic GetAttribution(int Id)
{
var attrib = new AttributedDto { ... };
// return objects directly, attributes do the rest.
return attrib;
}
// OR...
[HttpGet, Route("attribution/{Id:int}")]
public IHttpActionResult GetAttribution(int Id)
{
var attrib = new AttributedDto { ... };
// set the response using ASP.NET's ActionResult
// and use the object as the return value.
return Ok(attrib);
}
// OR...
[HttpGet, Route("attribution/{Id:int}")]
public IHttpActionResult GetAttribution(int Id)
{
var attrib = new AttributedDto { ... };
// wrap the object in a HalModel
return new HalModel(attrib)
...;
}
}
This project is a long term fork of https://github.com/visualeyes/halcyon in order to support a different methodology.