HAL.AspNetCore.OData 18.0.0

dotnet add package HAL.AspNetCore.OData --version 18.0.0                
NuGet\Install-Package HAL.AspNetCore.OData -Version 18.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="HAL.AspNetCore.OData" Version="18.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add HAL.AspNetCore.OData --version 18.0.0                
#r "nuget: HAL.AspNetCore.OData, 18.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install HAL.AspNetCore.OData as a Cake Addin
#addin nuget:?package=HAL.AspNetCore.OData&version=18.0.0

// Install HAL.AspNetCore.OData as a Cake Tool
#tool nuget:?package=HAL.AspNetCore.OData&version=18.0.0                

HAL

This project aims to bring a simple to use implementation of the Hypertext Application language.

Specification

Usage

The project consists of multiple packages

  1. HAL.Common which contains the Resource, Resource<T>, FormsResource, FormsResource<T> and Link implementations and the converters needed for serialization with System.Text.Json.
  2. 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.
  3. 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.
  4. 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.
  5. 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.

Without OData support

In Program.cs
builder.Services
    .AddControllers() // or .AddMvc()
    .AddHal()
    .AddJsonOptions(o => // not neccessary, but creates a much nicer output
    {
        o.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault; 
    });
In your controller
[Route("[controller]")]
public class MyController : HalControllerBase
{
    private readonly IResourceFactory _resourceFactory;

    public Table(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, ...
}

With OData support

In Program.cs with OData support
var modelBuilder = new ODataConventionModelBuilder();
modelBuilder.EntitySet<MyModelListDto>(typeof(MyModelListDto).Name);
builder.Services.AddSingleton(_ => modelBuilder.GetEdmModel());

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();
}
In your controller with OData support
[Route("[controller]")]
public class MyController : HalControllerBase
{
    private readonly IODataResourceFactory _resourceFactory;

    public Table(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, ...
}
Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on HAL.AspNetCore.OData:

Package Downloads
RESTworld.AspNetCore

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
18.0.0 107 11/20/2024
17.2.2 229 11/6/2024
17.2.1 403 10/11/2024
17.2.0 207 9/27/2024
17.1.2 320 7/30/2024
17.1.1 144 7/18/2024
17.1.0 288 7/3/2024
17.0.0 100 7/3/2024
16.0.0 111 7/2/2024
15.0.0 193 6/4/2024
14.1.0 684 1/9/2024
14.0.0 172 12/22/2023
13.1.0 352 11/27/2023
13.0.3 142 11/27/2023
13.0.2 133 11/27/2023
13.0.1 113 11/27/2023
13.0.0 236 11/14/2023
12.1.0 317 10/23/2023
12.0.0 133 10/23/2023
11.0.0 317 9/26/2023
10.5.1 213 9/5/2023
10.5.0 527 7/3/2023
10.4.1 257 6/28/2023
10.4.0 163 6/28/2023
10.3.0 462 6/14/2023
10.2.2 396 5/25/2023
10.2.1 247 5/16/2023
10.2.0 282 5/10/2023
10.1.0 288 4/30/2023
10.0.1 197 4/27/2023
10.0.0 272 4/18/2023
9.1.2 306 3/11/2023
9.1.1 618 2/22/2023
9.1.0 343 2/9/2023
9.0.0 769 12/12/2022
9.0.0-pre 166 11/21/2022
8.0.0 364 11/19/2022
7.0.0 583 11/9/2022
6.4.1 963 10/20/2022
6.4.0 754 9/27/2022
6.3.0 1,998 6/7/2022
6.2.1 447 6/8/2022
6.2.0 1,358 4/1/2022
6.1.0 769 3/30/2022
6.0.5 865 3/16/2022
6.0.4 2,633 3/7/2022
6.0.3 848 2/24/2022
6.0.2 1,456 2/21/2022
6.0.1 725 1/14/2022
6.0.0 981 12/3/2021
5.1.0 4,349 11/24/2021
5.0.2 1,330 10/12/2021
5.0.1 338 10/12/2021
5.0.0 514 10/6/2021
4.0.0 620 10/5/2021
3.0.0 1,065 9/7/2021
2.1.2 463 8/13/2021
2.1.1 800 6/28/2021
2.1.0 415 6/28/2021
2.0.0 353 6/28/2021
1.0.2 519 5/10/2021
1.0.1 970 3/31/2021
1.0.0 394 3/24/2021