Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asp.Net Core Set Default API Versioning

I'm using Asp.Net Core as a Rest API Service. I need to have API Versioning. Actually, I set in the Startup following settings and It's work correctly but when I set to default Version It's not working on.

services.AddVersionedApiExplorer(
                options =>
                    {
                        options.GroupNameFormat = "'v'VVV";
                        options.SubstituteApiVersionInUrl = true;
                        options.AssumeDefaultVersionWhenUnspecified = true;
                        options.DefaultApiVersion = new ApiVersion(1, 0);
                    });
services.AddApiVersioning(
                options =>
                    {
                        options.ReportApiVersions = true;
                        options.AssumeDefaultVersionWhenUnspecified = true;
                        options.DefaultApiVersion = new ApiVersion(1, 0);
                    })
            .AddMvc(
                options =>
                    {
                        options.RespectBrowserAcceptHeader = true;
                    })
            .AddXmlSerializerFormatters();

and set Attribute in Controllers like this: Version 1:

[ApiController]
[Route("v{version:apiVersion}/[controller]")]
[ApiVersion("1.0")]
public class UsersController : ControllerBase
{
    [HttpGet("log")]
    public string Get()
    {
        return $"{DateTime.Now}";
    }
}

Version 2:

[ApiController]
[Route("v{version:apiVersion}/[controller]")]
[ApiVersion("2.0")]
public class UsersController : ControllerBase
{
    [HttpGet("log")]
    public string Get()
    {
        return $"{DateTime.Now}";
    }
}

I can get the result as fllowing urls:

http://localhost:5000/v1/users/log => Status Code: 200

http://localhost:5000/v2/users/log => Status Code: 200

But http://localhost:5000/users/log => Status Code: 404

How I can set the default API in Versioning?

Thanks, everyone for taking the time to try and help explain

like image 738
Saeid Mirzaei Avatar asked Aug 27 '26 17:08

Saeid Mirzaei


1 Answers

Your configuration correctly sets default api version if none is specified. But your routes requires request url to include v{version} part. So one possible solution is to add another route without v{version} like this

[ApiController]
[Route("v{version:apiVersion}/[controller]")]
[Route("/[controller]")]
[ApiVersion("1.0")]
public class UsersController : ControllerBase
{
    [HttpGet("log")]
    public string Get()
    {
        return $"{DateTime.Now}";
    }
}

Now request to http://localhost:5000/users/log will point to controller with api version 1.0. If you add this route to second controller the version 1.0 will still be picked because default ApiVersion will be selected and it's 1.0.

like image 197
Alexander Avatar answered Aug 30 '26 13:08

Alexander



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!