Tapioca.HATEOAS 1.0.4

There is a newer version of this package available.
See the version list below for details.
dotnet add package Tapioca.HATEOAS --version 1.0.4                
NuGet\Install-Package Tapioca.HATEOAS -Version 1.0.4                
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="Tapioca.HATEOAS" Version="1.0.4" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Tapioca.HATEOAS --version 1.0.4                
#r "nuget: Tapioca.HATEOAS, 1.0.4"                
#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 Tapioca.HATEOAS as a Cake Addin
#addin nuget:?package=Tapioca.HATEOAS&version=1.0.4

// Install Tapioca.HATEOAS as a Cake Tool
#tool nuget:?package=Tapioca.HATEOAS&version=1.0.4                

Tapioca.HATEOAS

This is a smart library to implements HATEOAS pattern in your RESTFul API's.

URL of project

How to use

1 - Import Tapioca.HATEOAS to your projetct

Import with command line
Install-Package Tapioca.HATEOAS -Version 1.0.0
Import with nuget package manager

Nuget Package Mannager

2 - Implements ISupportsHyperMedia in your exposed object.

namespace RESTFulSampleServer.Data.VO
{
    public class BookVO : ISupportsHyperMedia
    {
        public long? Id { get; set; }
        public string Title { get; set; }
        public string Author { get; set; }
        public decimal Price { get; set; }
        public DateTime LaunchDate { get; set; }

        public List<HyperMediaLink> Links { get; set; } = new List<HyperMediaLink>();
    }
}

3 - Implements your enricher with ObjectContentResponseEnricher<T>.

namespace RESTFulSampleServer.HyperMedia
{
    public class BookEnricher : ObjectContentResponseEnricher<BookVO>
    {
        protected override Task EnrichModel(BookVO content, IUrlHelper urlHelper)
        {
            var path = "api/books/v1";
            var url = new { controller = path, id = content.Id };

            content.Links.Add(new HyperMediaLink()
            {
                Action = HttpActionVerb.GET,
                Href = urlHelper.Link("DefaultApi", url),
                Rel = RelationType.self,
                Type = RensponseTypeFormat.DefaultGet
            });
            content.Links.Add(new HyperMediaLink()
            {
                Action = HttpActionVerb.POST,
                Href = urlHelper.Link("DefaultApi", url),
                Rel = RelationType.self,
                Type = RensponseTypeFormat.DefaultPost
            });
            content.Links.Add(new HyperMediaLink()
            {
                Action = HttpActionVerb.PUT,
                Href = urlHelper.Link("DefaultApi", url),
                Rel = RelationType.self,
                Type = RensponseTypeFormat.DefaultPost
            });
            content.Links.Add(new HyperMediaLink()
            {
                Action = HttpActionVerb.DELETE,
                Href = urlHelper.Link("DefaultApi", url),
                Rel = RelationType.self,
                Type = "int"
            });
            return null;
        }
    }
}

4 - Add annotation [TypeFilter(typeof(HyperMediaFilter))] to your controller methods.

namespace RESTFulSampleServer.Controllers
{
    [ApiVersion("1")]
    [Route("api/[controller]/v{version:apiVersion}")]
    public class BooksController : Controller
    {
        private IBookBusiness _bookBusiness;

        public BooksController(IBookBusiness bookBusiness)
        {
            _bookBusiness = bookBusiness;
        }

        [HttpGet]
        //Add HyperMediaFilter
        [TypeFilter(typeof(HyperMediaFilter))]
        public IActionResult Get()
        {
            return new OkObjectResult(_bookBusiness.FindAll());
        }

        [HttpGet("{id}")]
        //Add HyperMediaFilter
        [TypeFilter(typeof(HyperMediaFilter))]
        public IActionResult Get(long id)
        {
            var book = _bookBusiness.FindById(id);
            if (book == null) return NotFound();
            return new OkObjectResult(book);
        }

        [HttpPost]
        //Add HyperMediaFilter
        [TypeFilter(typeof(HyperMediaFilter))]
        public IActionResult Post([FromBody]BookVO book)
        {
            if (book == null) return BadRequest();
            return new OkObjectResult(_bookBusiness.Create(book));
        }

        [HttpPut]
        //Add HyperMediaFilter
        [TypeFilter(typeof(HyperMediaFilter))]
        public IActionResult Put([FromBody]BookVO book)
        {
            if (book == null) return BadRequest();
            var updatedBook = _bookBusiness.Update(book);
            if (updatedBook == null) return BadRequest();
            return new OkObjectResult(updatedBook);
        }

        [HttpDelete("{id}")]
        //Add HyperMediaFilter
        [TypeFilter(typeof(HyperMediaFilter))]
        public IActionResult Delete(int id)
        {
            _bookBusiness.Delete(id);
            return NoContent();
        }
    }
}

5 - Add HyperMediaFilterOptions to your startup.

    var filtertOptions = new HyperMediaFilterOptions();
    filtertOptions.ObjectContentResponseEnricherList.Add(new BookEnricher());
    filtertOptions.ObjectContentResponseEnricherList.Add(new PersonEnricher());
    services.AddSingleton(filtertOptions);

6 - Add a MapRoute to your route like was defined in your enricher.

    app.UseMvc(routes =>
    {
        routes.MapRoute(
            name: "DefaultApi",
            template: "{controller=Values}/{id?}");
    });

7 - Enjoy

Response as JSON

Response As JSON

Response as XML

Response As XML

Suggestions are welcome

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  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. 
.NET Core netcoreapp2.0 is compatible.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
8.0.101.3 173 2/11/2024
8.0.101.2 120 2/11/2024
1.0.25 109 2/10/2024
1.0.24 135 2/10/2024
1.0.21 127 2/10/2024
1.0.19 136 2/10/2024
1.0.18 123 2/10/2024
1.0.17 112 2/10/2024
1.0.16 126 2/10/2024
1.0.15 116 2/10/2024
1.0.14 110 2/10/2024
1.0.13 97 2/10/2024
1.0.12 146 2/10/2024
1.0.11 122 2/10/2024
1.0.10 113 2/10/2024
1.0.9 118 2/10/2024
1.0.8 117 2/10/2024
1.0.7 113 2/10/2024
1.0.6 124 2/10/2024
1.0.5 121 2/9/2024
1.0.4 5,963 5/20/2018

This is a smart library to implements HATEOAS docummentation in your RESTFul API's