A simple HAL formatter for ASP.NET Web API.
A more in-depth usage sample should follow soon.
Install via NuGet:
install-package WebAPI.HALight
In your Startup.cs:
using using WebApi.HALight.Formatters;
// ...
var httpConfiguration = new HttpConfiguration();
// ... route config ...
// Add formatter for 'application/hal+json'.
httpConfiguration.Formatters.Add(new SimpleHalFormatter());
In your resource model:
using WebApi.HALight;
public class UserResource : Resource
{
public int Id { get; set; }
public string Name { get; set; }
}
In your controller:
public class UsersController : ApiController
{
private readonly UserRepository _repository;
// ...
// Route: "/Users/{id}"
public IHttpActionResult Get(int id)
{
var user = _repository.Get(id);
if (user == null)
{
return NotFound();
}
var userResource = new UserResource
{
Id = user.Id,
Name = user.Name,
};
userResource.Relations.Add(
Link.CreateSelfLink(
Url.Link(
"DefaultApi",
new { controller = "Users", id = userDetailResource.Id })
)
);
return Ok(userDetailResource);
}
}
There a couple of other HAL libraries for ASP .NET Web API around that inspired my take on this subject. A big thank you to: