![]() |
VOOZH | about |
dotnet add package HAL.AspNetCore.OData --version 20.0.0
NuGet\Install-Package HAL.AspNetCore.OData -Version 20.0.0
<PackageReference Include="HAL.AspNetCore.OData" Version="20.0.0" />
<PackageVersion Include="HAL.AspNetCore.OData" Version="20.0.0" />Directory.Packages.props
<PackageReference Include="HAL.AspNetCore.OData" />Project file
paket add HAL.AspNetCore.OData --version 20.0.0
#r "nuget: HAL.AspNetCore.OData, 20.0.0"
#:package HAL.AspNetCore.OData@20.0.0
#addin nuget:?package=HAL.AspNetCore.OData&version=20.0.0Install as a Cake Addin
#tool nuget:?package=HAL.AspNetCore.OData&version=20.0.0Install as a Cake Tool
A .NET 9 library for implementing the Hypertext Application Language (HAL) and HAL-Forms in web APIs and clients.
HAL is a standard for building discoverable, self-describing REST APIs using hypermedia links. This library helps you:
Use HAL if you want your API clients to discover available actions and related resources dynamically, following the HATEOAS principle.
This project provides:
HAL.Common which contains the Resource, Resource<T>, FormsResource, FormsResource<T> and Link implementations and the converters needed for serialization with System.Text.Json.HAL.AspNetCore adds IResourceFactory, IFormFactory and ILinkFactory which can be used in your controllers to easily generate resources from your models. It also comes with a HalControllerBase class which can be used for all Controllers which return HAL.HAL.AspNetCore.OData adds IODataResourceFactory and IODataFormFactory which can be used in your controllers to easily generate list endoints with paging from OData $filter, $skip and $top syntax.Hal.Client.Net is a client library to consume HAL APIs in .Net applications. When using it, you should call app.Services.AddHalClientFactoy() to inject the IHalClientFactory which can then be resolved in your application.Hal.Client.Angular/@wertzui/ngx-hal-client is a client library to consume HAL APIs in Angular applications. It exposes the HalClientModule which then provides the HalClient and a FormService.Add the desired package(s) via NuGet:
# Example for HAL.AspNetCore
dotnet add package HAL.AspNetCore
To use the Angular HAL client, install the npm package:
npm install @wertzui/ngx-hal-client
Use this approach for standard REST endpoints.
Configure services in Program.cs to enable HAL serialization:
builder.Services
.AddControllers()
.AddHal()
.AddJsonOptions(o =>
{
o.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault;
});
Return HAL resources from your controller:
[Route("[controller]")]
public class MyController : HalControllerBase
{
private readonly IResourceFactory _resourceFactory;
public MyController(IResourceFactory resourceFactory)
{
_resourceFactory = resourceFactory ?? throw new ArgumentNullException(nameof(resourceFactory));
}
[HttpGet]
[ApiConventionMethod(typeof(DefaultApiConventions), nameof(DefaultApiConventions.Get))]
public ActionResult<IResource> GetList()
{
var models = new[]
{
new MyModelListDto {Id = 1, Name = "Test1"},
new MyModelListDto {Id = 2, Name = "Test2"},
};
var result = _resourceFactory.CreateForListEndpoint(models, _ => "items", m => m.Id);
return Ok(result);
}
[HttpGet("{id}")]
public ActionResult<IResource<ModelFullDto>> Get(int id)
{
var model = new ModelFullDto { Id = id, Name = $"Test{id}", Description = "Very important!" };
var result = _resourceFactory.CreateForGetEndpoint(model);
return Ok(result);
}
// PUT, POST, ...
}
Use this approach if your API supports OData queries for filtering, paging, and sorting.
Configure services in Program.cs to enable OData and HAL:
builder.Services
.AddControllers(options => // or .AddMvc()
{
options.OutputFormatters.RemoveType<ODataOutputFormatter>();
options.InputFormatters.RemoveType<ODataInputFormatter>();
})
.AddOData()
.AddHALOData()
.AddJsonOptions(o => // not neccessary, but creates a much nicer output
{
o.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault;
});
var app = builder.Build();
app.UseRouting();
// ...
app.UseEndpoints(_ => { });
app.MapControllers();
Return HAL resources with OData support from your controller:
[Route("[controller]")]
public class MyController : HalControllerBase
{
private readonly IODataResourceFactory _resourceFactory;
public MyController(IODataResourceFactory resourceFactory)
{
_resourceFactory = resourceFactory ?? throw new ArgumentNullException(nameof(resourceFactory));
}
[HttpGet]
[ApiConventionMethod(typeof(DefaultApiConventions), nameof(DefaultApiConventions.Get))]
public ActionResult<Resource> GetList(
// The SwaggerIgnore attribute and all parameters beside the options are just here to give you a nice swagger experience.
// If you do not need that, you can remove everything except the options parameter.
// If you are using RESTworld, you can also remove everything except the options parameter, because there is a custom Swagger filter for that.
[SwaggerIgnore] ODataQueryOptions<TEntity> options,
[FromQuery(Name = "$filter")] string? filter = default,
[FromQuery(Name = "$orderby")] string? orderby = default,
[FromQuery(Name = "$top")] long? top = default,
[FromQuery(Name = "$skip")] long? skip = default)
{
var models = new[]
{
new MyModelListDto { Id = 1, Name = "Test1" },
new MyModelListDto { Id = 2, Name = "Test2" },
};
// Apply the OData filtering
models = options.Apply(models.AsQueryable()).Cast<MyModelListDto>().ToArray()
var result = _resourceFactory.CreateForOdataListEndpointUsingSkipTopPaging(models, _ => "items", m => m.Id, options);
return Ok(result);
}
// GET, PUT, POST, ...
}
Contributions are welcome! Please open issues or submit pull requests.
This project is licensed under the Unlicense.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 net10.0 is compatible. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
Showing the top 1 NuGet packages that depend on HAL.AspNetCore.OData:
| Package | Downloads |
|---|---|
|
RESTworld.AspNetCore
Package Description |
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 21.0.0 | 106 | 5/31/2026 |
| 20.0.5 | 212 | 3/12/2026 |
| 20.0.4 | 126 | 3/10/2026 |
| 20.0.3 | 260 | 2/10/2026 |
| 20.0.2 | 160 | 2/9/2026 |
| 20.0.1 | 440 | 12/15/2025 |
| 20.0.0 | 719 | 11/11/2025 |
| 19.0.2 | 289 | 11/9/2025 |
| 19.0.1 | 281 | 11/9/2025 |
| 19.0.0 | 277 | 11/9/2025 |
| 18.4.0 | 449 | 9/16/2025 |
| 18.3.0 | 435 | 6/19/2025 |
| 18.2.0 | 369 | 4/15/2025 |
| 18.1.0 | 383 | 3/17/2025 |
| 18.0.1 | 446 | 1/26/2025 |
| 18.0.0 | 471 | 11/20/2024 |
| 17.2.2 | 466 | 11/6/2024 |
| 17.2.1 | 658 | 10/11/2024 |
| 17.2.0 | 390 | 9/27/2024 |
| 17.1.2 | 561 | 7/30/2024 |