- Install the AspnetCore.Hal.SystemTextHalJsonFormatter package if we are using System.Text.Json
Install-Package AspnetCore.Hal.SystemTextHalJsonFormatter
Install the AspnetCore.Hal.NewtonsoftHalJsonFormatter package if we are using Newtonsoft
Install-Package AspnetCore.Hal.NewtonsoftHalJsonFormatter
- Create a
HalConfiguration
instance.
var config = new HalConfiguration();
//simple example - creates a "self" link templated with the user's id
config.For<UserSummary>()
.Links(model => new Link("self", "/users/{id}").CreateLink(model));
//complex example - creates paging links populated with query string search terms
config.For<PagedList<UserSummary>>()
.Embeds("users", x => x.Data)
.Links(
(model, ctx) =>
LinkTemplates.Users.GetUsersPaged.CreateLink("self", ctx.Request.Query, new { blah = "123" }))
.Links(
(model, ctx) =>
LinkTemplates.Users.GetUsersPaged.CreateLink("next", ctx.Request.Query, new { page = model.PageNumber + 1 }),
model => model.PageNumber < model.TotalPages)
.Links(
(model, ctx) =>
LinkTemplates.Users.GetUsersPaged.CreateLink("prev", ctx.Request.Query, new { page = model.PageNumber - 1 }),
model => model.PageNumber > 0);
//per request configuration
[ApiController]
[Route("[controller]")]
public class RolesController : Controller
{
private readonly IMapper _mapper;
public RolesController(IMapper mapper)
{
_mapper = mapper;
}
[HttpGet(Name = "GetRoles")]
public IEnumerable<Role> Get()
{
Database db=new Database(_mapper);
CreateTestDataIn(db);
var roles = db.GetAllRoles();
return roles;
}
}
- Register it in Program.cs of your application as below.
builder.Services.AddSingleton<IProvideHalTypeConfiguration>(provider => Halconfig.HypermediaConfiguration());
builder.Services.AddHalSupport();
- Set your
Accept
header toapplication/hal+json
This library could not exist without the work and ideas of Nancy.Hal