0

I am following below: https://www.hanselman.com/blog/ASPNETCoreRESTfulWebAPIVersioningMadeEasy.aspx

Is it possible to have directly higher version for a web api controller. like:

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

when using above one, it only works when using URL as api/v2.05/ger/getDetail But It fails working while using URL as api/ger/getDetail and getting message as "Code": "ApiVersionUnspecified",

If change version from 2.05 to 1.0 (as all other controller) then api/ger/getDetail works.

How to solve this, I need 2.05 for this controller and need to access api/ger/getDetail URL as well.

Thanks

user3711357
  • 1,425
  • 7
  • 32
  • 54

1 Answers1

1

Since you are versioning by URL segment, you'll have to do a few things. First in the options, you need to allow implicit versioning using:

options.AssumeDefaultVersionWhenUnspecified = true;

Your original API had some version that was never declared or named. The default configuration will use "1.0". If you want the default to be something else, specify:

options.DefaultApiVersion = new ApiVersion( 2, 0 );

The next step is that you have to float the route template on the controller that you want to have the default path. ASP.NET and all other stacks I know do not have a way to provide or fill-in default values in the middle of the route template.

If "2.0" is your initial, default version, then your controller will look like:

[ApiVersion( "2.0" )]
[ApiVersion( "2.05" )]
[RoutePrefix( "api" )]
public class GerController : ApiController
{
    [Route( "ger/getDetails" )]
    [Route( "v{version:apiVersion}/ger/getDetails" )]
    public IHttpActionResult GetDetails() => Ok();
}

If you change the controller that maps to the default route, you have to move the route template to that new controller type.

This is an unfortunate consequence of versioning by URL segment. If you don't change the default route mapping, then it shouldn't be a big deal to manage; otherwise, you should consider disallowing implicit versioning or elect an alternate versioning method.

For more information, please refer to this wiki topic.

Chris Martinez
  • 3,185
  • 12
  • 28