NancyHal / Nancy.Hal

Adds support for the Hal Media Type (and Hypermedia) to Nancy
MIT License
34 stars 12 forks source link

Announcement - Nancy.Hal is no longer being maintained! Since NancyFx is no longer being maintained, it doesn't make sense for Nancy.Hal to exist either. Thanks to all who contributed to this project.

Nancy.Hal NuGet Badge

Adds lightweight support for the Hal+JSON media type to Nancy

For Nancy 2.0.0-alpha support there is a pre-release package available.

What is Hal?

Specification

What Nancy.Hal does

What Nancy.Hal does not do

Get started

1) Install the Nancy.Hal package

Install-Package Nancy.Hal

2) 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
public ExampleModule()
{
    this.Get["/"] = _ => 
    {
        this.Context
            .LocalHalConfigFor<Users>()
            .Links("relation", "/link");

        return 200;
    };
}

3) Register it in your application container.

//TinyIOC
container.Register(typeof(IProvideHalTypeConfiguration), config);

//NInject
kernel.Bind<IProvideHalTypeConfiguration>().ToConstant(config);

4) That's it! Don't forget to set your Accept header to application/hal+json

Acknowledgements

This library could not exist without the work and ideas of others: