Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

An API version is required, but was not specified. webapi

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?

like image 916
user3711357 Avatar asked Mar 21 '18 20:03

user3711357


2 Answers

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.

like image 195
Chris Martinez Avatar answered Sep 19 '22 13:09

Chris Martinez


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.

like image 22
live-love Avatar answered Sep 19 '22 13:09

live-love