1

I'm in process of implementing HATEOAS principle in my WebAPI project. After a new entity of any given type is added to DB, response should contain not only a newly created entity, but 3 links to GET, PUT and DELETE endpoints in corresponding controller. Also, I use API versioning in endpoints URIs.

So, my controller is decorated with attributes:

[Route("api/v{version:apiVersion}/[controller]")]
[ApiController]
[ApiVersion("3.0")]

API versioning configuration in Startup.cs

services.AddApiVersioning(opt =>
{
    opt.AssumeDefaultVersionWhenUnspecified = true;
    opt.DefaultApiVersion = ApiVersion.Default;
    opt.ReportApiVersions = true;
});

Method to generate links for entity:

public override List<ResourceUri> GetResourceUris(int id, ApiVersion apiVersion)
{
    return new List<ResourceUri>
    {
        new ResourceUri
        {
            Method = "GET",
            Uri = this._linkGenerator.GetUriByAction(
                HttpContext, 
                nameof(GetResourceById), 
                values: new
                {
                    Id = id, 
                    area = "api", 
                    version = apiVersion.ToString()
                })
        },

        new ResourceUri
        {
            Method = "PUT",
            Uri = this._linkGenerator.GetUriByAction(HttpContext, nameof(UpdateItem), values: new { id })
        },

        new ResourceUri
        {
            Method = "DELETE",
            Uri = this._linkGenerator.GetUriByAction(HttpContext, nameof(DeleteItem), values: new { id })
        },

    };
}

Section

values: new
{
    Id = id, 
    area = "api", 
    version = apiVersion.ToString()
})

was taken from here, where it is marked as accepted answer.

However, every time I POST new entity, result is this:

{
    "uri": "https://localhost:44386/api/v1/Resources/43?area=api&version=3",
    "method": "GET"
}

Area and Version values are only included as query parameters and API version is always set to 'v1' instead of 'v3'.

Giving that

AssumeDefaultVersionWhenUnspecified 

is set to True, I'm using a wrong way to provide an ApiVersion to LinkGenerator. What should I do instead?

Eugene
  • 41
  • 8

1 Answers1

0

Just add

[Area("api")]

to the Controller

Eugene
  • 41
  • 8