Skip to content

olohmann/WebApi.HALight

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

WebAPI.HALight

A simple HAL formatter for ASP.NET Web API.

A more in-depth usage sample should follow soon.

Note: If you are looking for the ASP.NET Core version, move over to: Lohmann.HALight

Installation

Install via NuGet:

install-package WebAPI.HALight

Usage

In your Startup.cs:

using using WebApi.HALight.Formatters;

// ...
var httpConfiguration = new HttpConfiguration();

// ... route config ...

// Add formatter for 'application/hal+json'. 
httpConfiguration.Formatters.Add(new SimpleHalFormatter());

In your resource model:

using WebApi.HALight;

public class UserResource : Resource
{
    public int Id { get; set; }

    public string Name { get; set; }
}

In your controller:

public class UsersController : ApiController
{
    private readonly UserRepository _repository;

    // ...
    
    // Route: "/Users/{id}"
    public IHttpActionResult Get(int id)
    {
        var user = _repository.Get(id);

        if (user == null)
        {
            return NotFound();
        }

        var userResource = new UserResource
        {
            Id = user.Id,
            Name = user.Name,            
        };

        userResource.Relations.Add(
            Link.CreateSelfLink(
                Url.Link(
                    "DefaultApi", 
                    new { controller = "Users", id = userDetailResource.Id })
            )
        );

        return Ok(userDetailResource);
    }
}

Acknowledgements

There a couple of other HAL libraries for ASP .NET Web API around that inspired my take on this subject. A big thank you to:

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published