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:
Both URL working with v1 version ROUTE.
Second URL working for both, v1 and direct without v1 route as well i.e. [Route("~/api/gerDetail/getDetail")]
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?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With