HAL.AspNetCore.OData 2.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package HAL.AspNetCore.OData --version 2.0.0
NuGet\Install-Package HAL.AspNetCore.OData -Version 2.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="2.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 2.0.0
#r "nuget: HAL.AspNetCore.OData, 2.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=2.0.0

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

HAL

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

Specification

Usage

The project consists of three packages

  1. HAL.Common which contains the IResource, IResource<T> and ILink implementations and the converters needed for serialization with System.Text.Json.
  2. HAL.AspNetCore adds IResourceFactory and ILinkFactory which can be used in your controllers to easily generate resources from your models.
  3. HAL.AspNetCore.OData adds IODataResourceFactory which can be used in your controllers to easily generate list endoints with paging from OData $skip and $top syntax.

Without OData support

In Startup.cs
public void ConfigureServices(IServiceCollection services)
{
   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]")]
[ApiController]
public class MyController : ControllerBase
{
   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 Startup.cs
public void ConfigureServices(IServiceCollection services)
{
   services.AddOData();

   services
       .AddControllers() // or .AddMvc()
       .AddHALOData()
       .AddJsonOptions(o => // not neccessary, but creates a much nicer output
       {
           o.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault; 
       });
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
   // ...

   app.UseEndpoints(endpoints =>
   {
       endpoints.MapControllers();
       endpoints.EnableDependencyInjection();  // Required for OData to work.
   });

   // ...
}
In your controller
[Route("[controller]")]
[ApiController]
public class MyController : ControllerBase
{
  private readonly IODataResourceFactory _resourceFactory;

  public Table(IODataResourceFactory resourceFactory)
  {
      _resourceFactory = resourceFactory ?? throw new ArgumentNullException(nameof(resourceFactory));
  }

  [HttpGet]
  [ApiConventionMethod(typeof(DefaultApiConventions), nameof(DefaultApiConventions.Get))]
  public ActionResult<IResource> GetList()
  {
      // You may also create the OData model one time and reuse it.
      var modelBuilder = new ODataConventionModelBuilder();
      modelBuilder.EntitySet<MyModelListDto>(typeof(MyModelListDto).Name);
      var model = modelBuilder.GetEdmModel();
      var context = new ODataQueryContext(model, typeof(MyModelListDto), new ODataPath());
      var options = new ODataQueryOptions<MyModelListDto>(context, Request);

      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 net5.0 is compatible.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
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
14.1.0 500 1/9/2024
14.0.0 135 12/22/2023
13.1.0 309 11/27/2023
13.0.3 109 11/27/2023
13.0.2 104 11/27/2023
13.0.1 84 11/27/2023
13.0.0 202 11/14/2023
12.1.0 281 10/23/2023
12.0.0 96 10/23/2023
11.0.0 271 9/26/2023
10.5.1 173 9/5/2023
10.5.0 485 7/3/2023
10.4.1 208 6/28/2023
10.4.0 135 6/28/2023
10.3.0 419 6/14/2023
10.2.2 358 5/25/2023
10.2.1 212 5/16/2023
10.2.0 249 5/10/2023
10.1.0 239 4/30/2023
10.0.1 174 4/27/2023
10.0.0 238 4/18/2023
9.1.2 270 3/11/2023
9.1.1 573 2/22/2023
9.1.0 298 2/9/2023
9.0.0 706 12/12/2022
9.0.0-pre 135 11/21/2022
8.0.0 341 11/19/2022
7.0.0 539 11/9/2022
6.4.1 907 10/20/2022
6.4.0 708 9/27/2022
6.3.0 1,912 6/7/2022
6.2.1 408 6/8/2022
6.2.0 1,309 4/1/2022
6.1.0 728 3/30/2022
6.0.5 834 3/16/2022
6.0.4 2,528 3/7/2022
6.0.3 802 2/24/2022
6.0.2 1,392 2/21/2022
6.0.1 691 1/14/2022
6.0.0 903 12/3/2021
5.1.0 4,304 11/24/2021
5.0.2 1,245 10/12/2021
5.0.1 304 10/12/2021
5.0.0 487 10/6/2021
4.0.0 574 10/5/2021
3.0.0 996 9/7/2021
2.1.2 421 8/13/2021
2.1.1 751 6/28/2021
2.1.0 387 6/28/2021
2.0.0 313 6/28/2021
1.0.2 473 5/10/2021
1.0.1 906 3/31/2021
1.0.0 357 3/24/2021