Table of Contents

ApiVersionAliasParser Class

Definition

Namespace
Codebelt.Extensions.Asp.Versioning
Assemblies
Codebelt.Extensions.Asp.Versioning.dll
Source
src/Codebelt.Extensions.Asp.Versioning/ApiVersionAliasParser.cs

Resolves friendly API version aliases before delegating to another Asp.Versioning.IApiVersionParser.

public class ApiVersionAliasParser : IApiVersionParser
Inheritance
ApiVersionAliasParser
Implements
IApiVersionParser

Examples

Use ApiVersionAliasParser when callers send short version tokens such as 1, 1.2, or 1.2.3 over the wire but the rest of the application works with canonical SemanticApiVersion instances. The static CreateSemanticVersionAlias factory generates the alias map for a set of semantic versions, and Parse resolves a request token to the canonical version while the parser falls back to ApiVersionParser.Default for anything outside the alias table. The example below parses 1.2 and 2.0 from request strings and returns the resolved semantic versions.

using System;
using System.Collections.Generic;
using Asp.Versioning;
using Codebelt.Extensions.Asp.Versioning;

namespace Codebelt.Extensions.Asp.Versioning;

public class AliasAwareVersionResolver
{
    public IReadOnlyList<string> ResolveShortVersionTokens()
    {
        var stable = new SemanticApiVersion(1, 2, 0);
        var next = new SemanticApiVersion(2, 0, 0);

        IApiVersionParser parser = ApiVersionAliasParser.CreateSemanticVersionAlias([stable, next]);

        return new[]
        {
            parser.Parse("1.2").ToString(),
            parser.Parse("2.0").ToString()
        };
    }
}

Remarks

Use this parser when callers should be able to request a version by a shortened or compatibility-oriented token, while the application still works with the canonical Asp.Versioning.ApiVersion instance. Alias matching is performed before the fallback parser is invoked.

Constructors

Name Description
ApiVersionAliasParser(IReadOnlyDictionary<string, ApiVersion>)

Initializes a new instance of the ApiVersionAliasParser class that falls back to Asp.Versioning.ApiVersionParser.Default.

ApiVersionAliasParser(IReadOnlyDictionary<string, ApiVersion>, IApiVersionParser)

Initializes a new instance of the ApiVersionAliasParser class with the specified alias map and fallback parser.

Methods

Name Description
CreateSemanticVersionAlias(SemanticApiVersion)

Creates an API version parser that recognizes shortened aliases for a single semantic API version.

CreateSemanticVersionAlias(IEnumerable<SemanticApiVersion>)

Creates an API version parser that recognizes shortened aliases for the specified semantic API versions.

Parse(ReadOnlySpan<char>)

Parses the specified text into an API version by resolving aliases before using the fallback parser.

TryParse(ReadOnlySpan<char>, out ApiVersion)

Tries to parse the specified text into an API version by resolving aliases before using the fallback parser.

See Also

IApiVersionParser