I have inherited a legacy WebAPI system which currently uses underscores in the routing pattern to denote versions. For example /api/1_0/account
, /api/1_1/account
etc.
I am attempting to update the auto-generated documentation to use Swagger, however using explicit routing with ApiVersion
attributes which contain underscores leads to an exception. For example, this works fine:
[ApiVersion("1")]
However this throws an exception:
[ApiVersion("1_0")] // < note '_0' here
[RoutePrefix("api/{version:apiVersion}/account")]
public class AccountController : ApiBaseController
{
// actions...
}
The exception is:
FormatException: The specified API version status '_1' is invalid.
System.InvalidOperationException: 'Failed to compare two elements in the array.'
at System.Collections.Generic.ArraySortHelper`1.Sort(T[] keys, Int32 index, Int32 length, IComparer`1 comparer)
at System.Array.Sort[T](T[] array, Int32 index, Int32 length, IComparer`1 comparer)
at System.Collections.Generic.List`1.Sort(Int32 index, Int32 count, IComparer`1 comparer)
at Microsoft.Web.Http.Dispatcher.ApiVersionControllerSelector.InitializeControllerInfoCache()
at System.Lazy`1.CreateValue()
at System.Lazy`1.LazyInitValue()
at System.Lazy`1.get_Value()
at Microsoft.Web.Http.Dispatcher.ApiVersionControllerSelector.GetControllerMapping()
at System.Web.Http.Routing.AttributeRoutingMapper.AddRouteEntries(SubRouteCollection collector, HttpConfiguration configuration, IInlineConstraintResolver constraintResolver, IDirectRouteProvider directRouteProvider)
at System.Web.Http.Routing.AttributeRoutingMapper.<>c__DisplayClass1_1.b__1()
at System.Web.Http.Routing.RouteCollectionRoute.EnsureInitialized(Func`1 initializer)
at System.Web.Http.Routing.AttributeRoutingMapper.<>c__DisplayClass1_0.b__0(HttpConfiguration config)
at System.Web.Http.HttpConfiguration.EnsureInitialized()
at ProjectName.Startup.Configuration(IAppBuilder app) in E:\ProjectPath\Foo.cs:line 25
The issue is obvious, but how can I include the underscore in the version attribute value? The problem is confusing as I am assuming that the innards of the class are (at some point) parsing the value to an integer, yet the attribute itself accepts a string...? So why would that be?