5
var constraintResolver = new DefaultInlineConstraintResolver()
{
    ConstraintMap =
    {
        ["apiVersion"] = typeof( ApiVersionRouteConstraint )
    }
};
                
config.MapHttpAttributeRoutes(constraintResolver);
config.AddApiVersioning(o => o.AssumeDefaultVersionWhenUnspecified = true);


[ApiVersion("2.05")]
[RoutePrefix("api/v{version:apiVersion}/ger")]
public class caGerController
[Route("~/api/ger/getDetail")]
[Route("getDetail")]
 GetGerData


[ApiVersion("1")]
[RoutePrefix("api/v{version:apiVersion}/gerDetail")]

public class caGerDetailsController
caGerController
[Route("~/api/gerDetail/getDetail")]
[Route("getDetail")]
 GetGerData
   
>>  GetGerData

Result:

  1. Both URL working with v1 version ROUTE.

  2. Second URL working for both, v1 and direct without v1 route as well i.e. [Route("~/api/gerDetail/getDetail")]

  3. PROBLEM: first URL is only working with v1 and its not working with direct route like " [Route("~/api/ger/getDetail")]" and getting an error as below:

    "Error": { "Code": "ApiVersionUnspecified", "Message": "An API version is required, but was not specified." }

How to solve this issue? When I change from 2.05 to 1.0 then it works but 2.0 or 2.05 both do not work. Is there a separate folder required?

live-love
  • 48,840
  • 22
  • 240
  • 204
user3711357
  • 1,425
  • 7
  • 32
  • 54

2 Answers2

13

The ApiVersionUnspecified happens because all routes require an explicit API version by default. You opt out of this behavior using:

options.AssumeDefaultVersionWhenUnspecified = true

This setting means that a default API version is assumed when a client doesn't provide one. The default value is:

options.DefaultApiVersion // equals 1.0 by default

When you use the URL segment versioning method, you can't have two different controllers that both an unversioned route. The route without an API version can only map to a single controller. Since the default is "1.0" and you have a controller with the unversioned route, that's the one that will always been matched.

Chris Martinez
  • 3,185
  • 12
  • 28
  • 1
    How do you specify a version? – Jason Cheng Feb 25 '19 at 22:34
  • 1
    _How_ you specify an API version depends on the style of versioning you are using. In the original question, the URL segment method is being used. This means that the request specifies the API version in the form of `api/v1/ger`. Using this style of versioning, if you want a _default_ version, then you'll need a second route for it (e.g. `api/ger`). With both routes and `options.AssumeDefaultVersionWhenUnspecified = true` set, the routes `api/v1/ger` and `api/ger` with both go to the same implementation. – Chris Martinez Feb 27 '19 at 01:11
3

By adding API versioning, the default behavior is that it uses QueryString versioning.

   config.AddApiVersioning(cfg => {});

api-version=1.0

To specify a version, you can add the querystring parameter api-version=1.0 at the end.

Example:

http://localhost:6600/api/test?api-version=1.0

You can change the version like this:

protected void Application_Start()
    {
      AreaRegistration.RegisterAllAreas();
      GlobalConfiguration.Configure(WebApiConfig.Register);
...

    public static void Register(HttpConfiguration config)
    {
       ...
      config.AddApiVersioning(cfg =>
      {
         cfg.DefaultApiVersion = new ApiVersion(1,1);
      });

So you can change the version like this:

http://localhost:6600/api/test?api-version=1.1

By adding AssumeDefaultVersionWhenUnspecified, you don't have to specify a version.

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

This will work: http://localhost:6600/api/test

You can also add ReportApiVersions

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

The response will have a new header api-supported-versions which specifies what versions are supported for the call they made.

live-love
  • 48,840
  • 22
  • 240
  • 204