HAL.Client.Net 7.2.0

dotnet add package HAL.Client.Net --version 7.2.0
NuGet\Install-Package HAL.Client.Net -Version 7.2.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.Client.Net" Version="7.2.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add HAL.Client.Net --version 7.2.0
#r "nuget: HAL.Client.Net, 7.2.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.Client.Net as a Cake Addin
#addin nuget:?package=HAL.Client.Net&version=7.2.0

// Install HAL.Client.Net as a Cake Tool
#tool nuget:?package=HAL.Client.Net&version=7.2.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]")]
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 Startup.cs
public void ConfigureServices(IServiceCollection services)
{
   services.AddOData();

   var modelBuilder = new ODataConventionModelBuilder();
   modelBuilder.EntitySet<MyModelListDto>(typeof(MyModelListDto).Name);
   services.AddSingleton(_ => modelBuilder.GetEdmModel());

   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]")]
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.
          [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 net8.0 is compatible.  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.Client.Net:

Package Downloads
RESTworld.Client.Net

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
7.2.0 465 1/9/2024
7.1.0 124 12/22/2023
7.0.0 364 11/14/2023
6.1.0 278 10/23/2023
6.0.0 105 10/23/2023
5.0.0 267 9/26/2023
4.3.0 392 7/11/2023
4.2.0 165 7/11/2023
4.1.0 201 7/3/2023
4.0.2 404 6/14/2023
4.0.1 150 5/25/2023
4.0.0 417 5/16/2023
3.2.0 246 4/30/2023
3.1.2 316 3/11/2023
3.1.1 573 2/22/2023
3.1.0 291 2/9/2023
3.0.0 594 12/12/2022
2.0.0 470 11/9/2022
1.3.1 636 10/20/2022
1.3.0 536 9/27/2022
1.2.2 746 6/9/2022
1.2.1 541 6/9/2022
1.2.0 541 6/8/2022
1.1.5 421 6/8/2022
1.1.4 553 6/7/2022
1.1.3 418 6/7/2022
1.1.2 426 6/7/2022
1.1.1 433 6/7/2022
1.1.0 417 6/7/2022
1.0.0 406 6/3/2022