Hal.Core is a robust library designed to streamline the implementation of Hypertext Application Language (HAL) in .NET applications. It provides a foundation for creating HAL-compliant APIs, enhancing the readability and maintainability of your code.
- Resource Building: Simplifies the creation of HAL resources with embedded links.
- Link Generation: Easily generate and manage hyperlinks within your resources.
- Collection Handling: Manage collections of resources effectively with built-in utilities.
- Extensibility: Customizable to fit various use cases.
Install the package via NuGet:
dotnet add package Hal.Core
Here's a basic example of how to use Hal.Core in your .NET application:
using Hal.Core;
var forecast = Enumerable.Range(1, 5).Select(index =>
new WeatherForecast
(
DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
Random.Shared.Next(-20, 55),
summaries[Random.Shared.Next(summaries.Length)]
))
.ToArray();
var response = new ResourceCollectionBuilder<WeatherForecast, string>(forecast, "meta")
.AddLink(linkGenerator
.SetRouteName("GetWeatherForecast")
.Build())
.Build();
VHal.AspNetCore provides ASP.NET Core extensions for implementing Hypertext Application Language (HAL) in web APIs. This package integrates seamlessly with Hal.Core to offer a comprehensive solution for HAL-compliant API development.
- ASP.NET Core Integration: Smooth integration with ASP.NET Core, simplifying the creation of HAL resources.
- Request Handling: Efficient handling of requests and responses within the HAL framework.
- Resource Link Management: Streamlined link management for resource collections and individual resources.
Install the package via NuGet:
dotnet add package VHal.AspNetCore
Here's how to use VHal.AspNetCore in your ASP.NET Core application:
using VHal.AspNetCore;
using Microsoft.AspNetCore.Mvc;
[ApiController]
[Route("api/[controller]")]
public class WeatherForecastController : ControllerBase
{
private readonly ILinkGenerator linkGenerator;
public WeatherForecastController(ILinkGenerator linkGenerator)
{
this.linkGenerator = linkGenerator;
}
[HttpGet]
public IActionResult GetWeatherForecast()
{
var forecast = Enumerable.Range(1, 5).Select(index =>
new WeatherForecast
(
DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
Random.Shared.Next(-20, 55),
summaries[Random.Shared.Next(summaries.Length)]
))
.ToArray();
var response = new ResourceCollectionBuilder<WeatherForecast, string>(forecast, "meta")
.AddLink(linkBuilder =>
linkBuilder
.SetRel("self")
.SetMethod(HttpVerbs.Get)
.SetHref(linkGenerator.GenerateUri("GetWeatherForecastWithMeta", new { }))
.Build())
.AddLink(linkBuilder =>
linkBuilder
.SetRel("all")
.SetMethod(HttpVerbs.Get)
.SetHref(linkGenerator.GenerateUri("GetWeatherForecast", new { }))
.Build())
.Build();
return Ok(response);
}
[HttpGet("{id}")]
public IActionResult GetWeatherForecastById(int id)
{
var item = new WeatherForecast
(
DateOnly.FromDateTime(DateTime.Now.AddDays(id)),
Random.Shared.Next(-20, 55),
summaries[Random.Shared.Next(summaries.Length)]
);
var response = new ResourceBuilder<WeatherForecast>(item)
.AddLink("self", linkGenerator.GenerateUri("GetLowest", new { }), HttpVerbs.Get)
.AddLink("all", linkGenerator.GenerateUri("GetWeatherForecast", new { }), HttpVerbs.Get)
.Build();
return Ok(response);
}
}
This project is licensed under the MIT License.