Web-API Versioning not working with Default version

Web-API Versioning not working with Default version

addapiversioning
odata api versioning
asp.net core 2.2 api versioning
api versioning net core
web api versioning best practices
web api multiple versions
api route versioning
apiversion attribute

I created one web API application with versioning. I am going to use Microsoft.AspNet.WebApi.Versioning package to do that.

Webapi configuration:

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // Web API configuration and services
        config.AddApiVersioning(o => {
            o.AssumeDefaultVersionWhenUnspecified = true;
        });
        // Web API routes
        config.MapHttpAttributeRoutes();

    }
}

Here my assumption is if I didn't pass the version, by default it will select version 1.

Controller code:

[ApiVersion("1.0")]
[RoutePrefix("api/users")]
public class UserV1Controller : BaseController
{
    public UserV1Controller()
    {
    }

    [Route("all", Name = "UsersCollection")]
    public async Task<IHttpActionResult> GetAll()
    {
        var items = await UnitOfWork.Users.GetPaged(x => x.OrderBy(y => y.Id), 1, 20);
        //Add mapping to DTO 
        return Ok(items);
    }

}

If I test with http://localhost:10280/api/users/all?api-version=1.0 URL, it working fine. I am going to implement this feature in the existing project.

For backward compatibility, I tried http://localhost:10280/api/users/all URL. It gives me the following error with 500 as the status code.

{ "Message": "An error has occurred.", "ExceptionMessage": "The index cannot be less than 0 or equal to or larger than the number of items in the collection.\r\nParameter name: index\r\nActual value was 0.", "ExceptionType": "System.ArgumentOutOfRangeException", "StackTrace": " at System.Web.Http.WebHost.Routing.HostedHttpRouteCollection.get_Item(Int32 index)\r\n at Microsoft.Web.Http.Dispatcher.ApiVersionControllerSelector.GetControllerName(HttpRequestMessage request)\r\n at Microsoft.Web.Http.Dispatcher.ControllerSelectionContext.<>c__DisplayClass6_0.<.ctor>b__0()\r\n at System.Lazy1.CreateValue()\r\n at System.Lazy1.LazyInitValue()\r\n at System.Lazy`1.get_Value()\r\n at Microsoft.Web.Http.Dispatcher.ConventionRouteControllerSelector.SelectController(ControllerSelectionContext context)\r\n at Microsoft.Web.Http.Dispatcher.ApiVersionControllerSelector.SelectController(HttpRequestMessage request)\r\n at System.Web.Http.Dispatcher.HttpControllerDispatcher.d__15.MoveNext()" }


Update 1:

After discussions and a bit of help, I can confirm that this default version works with conventional routing.

The issue reproduced while using attribute routing. Please check my updated code.

  1. I don't have any default API routing in the WebAPIConfig file.
  2. I update Route Prefix and Route in the controller

Now the issue reproduced.

You can also reproduce the issue with combining conventional and attribute.

Update 2:

Now I noticed that the issue lies in the configuration. If I add the routing configurations in the Owin startup class directly it working fine. 

    public partial class Startup
{
    public void Configuration(IAppBuilder app)
    {
        ConfigureAuth(app);
        var configuration = new System.Web.Http.HttpConfiguration();
        var httpServer = new System.Web.Http.HttpServer(configuration);

        // reporting api versions will return the headers "api-supported-versions" and "api-deprecated-versions"
        configuration.AddApiVersioning(options =>
        {
            options.ReportApiVersions = true;
            options.AssumeDefaultVersionWhenUnspecified = true;
        });
        configuration.MapHttpAttributeRoutes();
        app.UseWebApi(httpServer);

    }
}

It creates new HttpConfiugration other than using our existing WebAPIConfig class. So I don't know it may impact any other functionalities

So if i configure Owin to use webaPI instead of GlobalConfiguration.Configure(WebApiConfig.Register) in the Global.asax, it iw working fine.


This is a confirmed bug that only occurs when hosting on a vanilla IIS implementation (e.g. non-OWIN). Despite the HttpRouteCollection having an indexer, the HostedHttpRouteCollection defined by System.Web does not support it. Instead of throwing NotSupportedException, a generic ArgumentOutOfRangeException is thrown.

For more details see: https://github.com/Microsoft/aspnet-api-versioning/issues/428.

The fix is in and is now available in package version 3.0.1.

Default version Web-API Versioning not working with Attribute , I already have one Web API project. Now I am planning to use versioning with Microsoft.AspNet.WebApi.Versioning . Before upgrading, I  Default version Web-API Versioning not working with Attribute Routing [Answered] RSS 1 reply Last post Jan 04, 2019 03:58 AM by akhilrajau


If you do not specify a version it is considered unversioned. In this case you have to set the AssumeDefaultVersionWhenUnspecified property, which if not told otherwise, assumes a default version of "1.0", if none is specified:

config.AddApiVersioning(o => {
    o.AssumeDefaultVersionWhenUnspecified = true;
});

This functionality is documented here: https://github.com/Microsoft/aspnet-api-versioning/wiki/Existing-Services-Quick-Start

AssumeDefaultVersionWhenUnspecified is not working as expected , I have been using asp net core versioning component for my WebAPI. the default routing reaches the API (when the version is not specified  Great work. We followed versioning with custom service class extending DefaultHttpControllerSelector class. After reading this, I am planning to change the versioning using this package. I created a very simple WebAPI project to test thi


You can try setting up the default version number and configure middleware to use it when version number is not specified.

Reference: Scott's blog on routing

services.AddApiVersioning(
    o =>
    {
        o.AssumeDefaultVersionWhenUnspecified = true );
        o.DefaultApiVersion = new ApiVersion("1.0");
    } );

Default API version · Issue #168 · microsoft/aspnet-api-versioning , Is there a way to get default API version in code? NET Web API When I use a mix of API and MVC controllers, Url.Action does not work. The default method is to use a query string parameter named api-version. When no version is mentioned, the client will get the latest version configured in the startup. You can change the default parameter name while configuring it in the startup class. We will see an example of custom parameter names later in this post.


Versioning Web API, I will talk about versioning in general and versioning Web API using multiple in a way such that clients using that feature might not work anymore. The default method is to use a query string parameter named api-version. 1. Introduction. When you start versioning your API sometimes you want to have access to your endpoints without specifying version explicitly. Thanks to Microsoft.AspNetCore.Mvc.Versioning library this can be easily achieved with proper setup of configuration action. However, it turns out that in order to enable default API version for url path versioning, there are a …


API Versioning in ASP.NET Core, version ASP.NET Core Web API and some different ways of versioning the API. consumers? We can solve this problem by versioning our API. Line #6 - DefaultApiVersion is used to set the default version to API. Line #7  Since the API version was not specified in the request, the default version of 1.0 is assumed. Furthermore, values controller is not specified with any version number, the values controller is also assumed to be of the default version. Advertise the accepted API Version to the client


Four REST API Versioning Strategies, When we release a new major version of the xMatters REST API, clients can Clients may not want to update their applications when the API changes, so a versioning their code and work on better representations for the resources of their API.” It is also easy to default to the latest version if a query parameter is not  The default API versioning configuration is compliant with the versioning semantics outlined by the Microsoft REST Guidelines. There are also a number of customization and extension points available to support transitioning services that may not have supported API versioning in the past or supported API versioning with semantics that are