Skip to content

Url Helpers not working with the api version in URL #615

@Netclick19

Description

@Netclick19

Hi,
in my API I have some problem with URL helpers with .NET core 3.1.

In startup.cs I set up the api versioning on this way:

services.AddApiVersioning(options =>
            {
                options.DefaultApiVersion = new ApiVersion(1,0);
                options.AssumeDefaultVersionWhenUnspecified = true;
                options.ApiVersionSelector = new CurrentImplementationApiVersionSelector(options);
                options.Conventions.Add(new VersionByNamespaceConvention());
            });

This is my controller.

    [ApiVersion("1.0")]
    [Route("api")]
    //[Route("api/v1/[controller]")]
    [ApiController]
    [Produces("application/json")]
    public class ColorController : Controller

This is my 2 actions.

        [Route("v{version:apiVersion}/[controller]/search", Name = nameof(GetColors))]
        //[Route("get", Name = nameof(GetColor))]
        [HttpGet]
        [ProducesResponseType(StatusCodes.Status200OK)]
        [ProducesResponseType(StatusCodes.Status404NotFound)]
        public IActionResult GetColors()

This is my post action

        [Route("v{version:apiVersion}/[controller]/add", Name = nameof(AddColor))]
        [HttpPost]
        public IActionResult AddColor([FromBody] ColoreEntity objectToAdd)

var url = Url.RouteUrl(nameof(GetColor), new {objectToAdd.idColor});
The url variable is empty.

return CreatedAtRoute(nameof(GetColor), new { idColor = objectToAdd.idColore }, objectToAdd);
this piece of code launch this exception
no route matches the supplied values

It is a bug or probably my mistake?
Thank you for your support.

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions