Lyo.ShortUrl 3.0.0

dotnet add package Lyo.ShortUrl --version 3.0.0
                    
NuGet\Install-Package Lyo.ShortUrl -Version 3.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Lyo.ShortUrl" Version="3.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Lyo.ShortUrl" Version="3.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Lyo.ShortUrl" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Lyo.ShortUrl --version 3.0.0
                    
#r "nuget: Lyo.ShortUrl, 3.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Lyo.ShortUrl@3.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Lyo.ShortUrl&version=3.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Lyo.ShortUrl&version=3.0.0
                    
Install as a Cake Tool

Lyo.ShortUrl

URL-shortening contracts: IShortUrlService, ShortUrlServiceBase for validation / metrics / error-code mapping, a default ShortUrlService that mints short codes (no storage), a fluent UrlShortenBuilder, and DTOs for shorten / expand / statistics results.

Examples

Quick start

using Lyo.ShortUrl;
using Lyo.ShortUrl.Models;

services.AddShortUrl(o => {
    o.BaseUrl = "https://short.ly";
    o.AllowCustomAliases = true;
    o.MaxAliasLength = 50;
});

var builder = UrlShortenBuilder.New()
    .SetLongUrl("https://example.com/long-url")
    .SetCustomAlias("my-alias")
    .SetExpirationDate(DateTime.UtcNow.AddDays(30));

var result = await shortUrlService.ShortenAsync(builder, ct);
// result.IsSuccess, result.ShortUrl, result.LongUrl, result.Alias, result.Errors

// Expand / stats / update / delete require a storage-backed IShortUrlService
// implementation; the in-box ShortUrlService surfaces NotSupported errors there.

Configuration (appsettings.json)

{
  "ShortUrlOptions": {
    "BaseUrl": "https://short.ly",
    "DefaultExpirationDays": 30,
    "MaxAliasLength": 50,
    "MinAliasLength": 3,
    "AllowCustomAliases": true,
    "EnableMetrics": false,
    "EnforceHttps": false
  }
}

IShortUrlService

  • Task<UrlShortenResult> ShortenAsync(string longUrl, string? customAlias = null, DateTime? expirationDate = null, CancellationToken ct = default)
  • Task<UrlShortenResult> ShortenAsync(UrlShortenBuilder builder, CancellationToken ct = default)
  • Task<UrlExpandResult> ExpandAsync(string shortUrl, CancellationToken ct = default)
  • Task<UrlStatisticsResult> GetStatisticsAsync(string shortUrl, CancellationToken ct = default)
  • Task<bool> DeleteAsync(string shortUrl, CancellationToken ct = default)
  • Task<UrlShortenResult> UpdateAsync(string shortUrl, string newLongUrl, CancellationToken ct = default)
  • Task<bool> TestConnectionAsync(CancellationToken ct = default)

ShortUrlServiceBase

  • Validates inputs (longUrl non-empty; expirationDate strictly in the future; HTTP → HTTPS rewrite when Options.EnforceHttps == true).
  • Wraps ShortenAsync / ExpandAsync in metrics timers plus counters keyed off MetricNames. Override CreateMetricNamesDictionary() to rebrand.
  • Maps OperationCanceledException → SHORTURL_OPERATION_CANCELLED, other exceptions → SHORTURL_*_FAILED codes.
  • Supplies default NotSupportedException throws for GetStatisticsAsync, DeleteAsync, UpdateAsync, TestConnectionAsync, ShortenCoreAsync, and ExpandCoreAsync, so a partial implementation only overrides what it supports.

ShortUrlService (in-box)

  • ShortenCoreAsync checks the custom alias against Options.AllowCustomAliases / MinAliasLength / MaxAliasLength and, when none is supplied, calls IShortUrlGenerator.Generate. It then returns UrlShortenResult.FromSuccess(...) with BaseUrl/{id} (or just {id} when BaseUrl is empty).
  • ExpandCoreAsync, GetStatisticsAsync, and UpdateAsync return an error result with SHORTURL_EXPAND_FAILED / SHORTURL_GET_STATISTICS_FAILED / SHORTURL_UPDATE_FAILED. They require a storage-backed implementation.
  • DeleteAsync throws NotSupportedException for the same reason.
  • TestConnectionAsync always returns true. There is no backend to probe.

IShortUrlGenerator / ShortUrlGenerator

  • string Generate(int? length = null) returns a base-62 string (a-zA-Z0-9) of the requested length. The default ShortUrlGenerator uses RandomNumberGenerator and an 8-character default length.

UrlShortenBuilder

  • SetLongUrl(string longUrl, bool enforceHttps = false) runs through UriHelpers.GetValidWebUri (throws InvalidFormatException on invalid URLs). When enforceHttps, HTTP becomes HTTPS.
  • SetCustomAlias(string? alias) must match ^[a-zA-Z0-9\-]+$. Passing null/whitespace clears the alias.
  • SetExpirationDate(DateTime? date) must be in the future, or null.
  • Clear() resets the builder.
  • Build() returns (LongUrl, CustomAlias, ExpirationDate). Throws if LongUrl was never set.
  • UrlShortenBuilder.New() convenience factory.

Error codes (ShortUrlErrorCodes)

SHORTURL_SHORTEN_FAILED, SHORTURL_EXPAND_FAILED, SHORTURL_GET_STATISTICS_FAILED, SHORTURL_DELETE_FAILED, SHORTURL_UPDATE_FAILED, SHORTURL_OPERATION_CANCELLED, SHORTURL_URL_NOT_FOUND, SHORTURL_URL_EXPIRED, SHORTURL_INVALID_URL, SHORTURL_ALIAS_ALREADY_EXISTS, SHORTURL_CUSTOM_ALIAS_NOT_ALLOWED, SHORTURL_INVALID_ALIAS_LENGTH.

ShortUrlServiceOptions

Member Default Notes
BaseUrl empty Prepended to generated ids (for example https://short.ly).
DefaultExpirationDays null Reserved for storage-backed implementations.
MaxAliasLength 50 Enforced by ShortUrlService.ShortenCoreAsync.
MinAliasLength 3 Enforced by ShortUrlService.ShortenCoreAsync.
AllowCustomAliases true When false, custom aliases are rejected with CUSTOM_ALIAS_NOT_ALLOWED.
EnableMetrics false Gates the IMetrics integration in ShortUrlServiceBase.
EnforceHttps false When true, HTTP URLs are rewritten to HTTPS before hitting ShortenCoreAsync.
SectionName (const) "ShortUrlOptions" Default appsettings section for AddShortUrlFromConfiguration.

Service registration (Extensions)

Entry point Effect
services.AddShortUrlGenerator() Registers singleton IShortUrlGenerator → ShortUrlGenerator.
services.AddShortUrl(Action<ShortUrlServiceOptions>? configure = null) (plus an (options) overload) Registers ShortUrlServiceOptions, the generator, and singleton IShortUrlService → ShortUrlService.
services.AddShortUrlFromConfiguration(IConfiguration configuration, string sectionName = ShortUrlServiceOptions.SectionName) Same as AddShortUrl(...) but binds options from the configuration section (default "ShortUrlOptions").
services.AddShortUrlService<TService, TOptions>(Action<TOptions>? configure = null) Generic registration for a custom IShortUrlService + TOptions : ShortUrlServiceOptions, new() pair.
services.AddShortUrlService<TService>(ShortUrlServiceOptions options) The same with a pre-built options instance.

Dependencies

Generated from ProjectReference / PackageReference (same model as docs/Lyo.ProjectGraph.html).

  • Lyo.Common.Core (direct, lyo)
  • Lyo.Exceptions (direct, lyo)
  • Lyo.Metrics (direct, lyo)
  • Lyo.Result (direct, lyo)
  • Microsoft.Extensions.Configuration.Binder 10.0.5 (direct, microsoft)
  • Microsoft.Extensions.DependencyInjection.Abstractions 10.0.5 (direct, microsoft)
  • Microsoft.Extensions.Logging.Abstractions 10.0.5 (direct, microsoft)
  • Microsoft.Extensions.Options 10.0.5 (direct, microsoft)
  • Microsoft.Bcl.AsyncInterfaces 10.0.5 (transitive, microsoft, netstandard2.0)
  • System.Memory 4.6.3 (transitive, microsoft, netstandard2.0)
  • System.Text.Json 10.0.5 (transitive, microsoft, netstandard2.0)
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on Lyo.ShortUrl:

Package Downloads
Lyo.ShortUrl.Postgres

PostgreSQL implementation of Lyo.ShortUrl using Entity Framework Core.

Lyo.Leaderboard.Api

HTTP API for Lyo.Leaderboard. Embed via AddLeaderboardApi / MapLeaderboardApiEndpoints.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
3.0.0 124 10/23/2026
2.0.1 144 9/14/2026
2.0.0 136 9/9/2026
1.0.13 141 8/25/2026
1.0.11 141 8/23/2026
1.0.9 144 8/22/2026
1.0.6 140 8/20/2026
1.0.4 147 8/20/2026
1.0.3 141 8/19/2026
1.0.2 143 8/19/2026
1.0.1 140 8/18/2026
1.0.0 144 8/16/2026