Home > Back-end >  How to set 1.1 API version as default in Swagger
How to set 1.1 API version as default in Swagger

Time:04-27

I have such settings that set my v1 as default:

services.AddApiVersioning(config =>
{
    config.DefaultApiVersion = new ApiVersion(1, 0);
    config.AssumeDefaultVersionWhenUnspecified = true;
});

How can I set 1.1 to DefaultApiVersion? There are no string, double or float constructor of ApiVersion

CodePudding user response:

I'm guessing you misunderstood this setting.

Are you setting like this:

services.AddApiVersioning(config =>
{
    config.DefaultApiVersion = new ApiVersion(1.1, 0);
    config.AssumeDefaultVersionWhenUnspecified = true;
});

and it will report an error.

check the source code about ApiVersion, It is described like:

// Summary:
        //     Initializes a new instance of the Microsoft.AspNetCore.Mvc.ApiVersion class.
        //
        // Parameters:
        //   majorVersion:
        //     The major version.
        //
        //   minorVersion:
        //     The minor version.
        public ApiVersion(int majorVersion, int minorVersion)
            : this(null, majorVersion, minorVersion, null)
        {
        }

So, you just need to set:

//version 1.1
config.DefaultApiVersion = new ApiVersion(1, 1);

CodePudding user response:

Try to add this:

services.AddVersionedApiExplorer(options =>
    {
        options.GroupNameFormat = "'v'VVV";
        options.SubstituteApiVersionInUrl = true;
    });
  • Related