Skip to content

Adds support for the Hal Media Type (and Hypermedia) to ASP.Net Core

License

Notifications You must be signed in to change notification settings

thomsonreuters/AspNet.Core.Hal

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

98 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

What is Hal?

Specification

Get started

  1. 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
  1. 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;
        }
}
  1. Register it in Program.cs of your application as below.
builder.Services.AddSingleton<IProvideHalTypeConfiguration>(provider => Halconfig.HypermediaConfiguration());

builder.Services.AddHalSupport();
  1. Set your Accept header to application/hal+json

Acknowledgements

This library could not exist without the work and ideas of Nancy.Hal

About

Adds support for the Hal Media Type (and Hypermedia) to ASP.Net Core

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 100.0%