Lyo.ShortUrl
3.0.0
dotnet add package Lyo.ShortUrl --version 3.0.0
NuGet\Install-Package Lyo.ShortUrl -Version 3.0.0
<PackageReference Include="Lyo.ShortUrl" Version="3.0.0" />
<PackageVersion Include="Lyo.ShortUrl" Version="3.0.0" />
<PackageReference Include="Lyo.ShortUrl" />
paket add Lyo.ShortUrl --version 3.0.0
#r "nuget: Lyo.ShortUrl, 3.0.0"
#:package Lyo.ShortUrl@3.0.0
#addin nuget:?package=Lyo.ShortUrl&version=3.0.0
#tool nuget:?package=Lyo.ShortUrl&version=3.0.0
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 (
longUrlnon-empty;expirationDatestrictly in the future; HTTP → HTTPS rewrite whenOptions.EnforceHttps == true). - Wraps
ShortenAsync/ExpandAsyncin metrics timers plus counters keyed offMetricNames. OverrideCreateMetricNamesDictionary()to rebrand. - Maps
OperationCanceledException→SHORTURL_OPERATION_CANCELLED, other exceptions →SHORTURL_*_FAILEDcodes. - Supplies default
NotSupportedExceptionthrows forGetStatisticsAsync,DeleteAsync,UpdateAsync,TestConnectionAsync,ShortenCoreAsync, andExpandCoreAsync, so a partial implementation only overrides what it supports.
ShortUrlService (in-box)
ShortenCoreAsyncchecks the custom alias againstOptions.AllowCustomAliases/MinAliasLength/MaxAliasLengthand, when none is supplied, callsIShortUrlGenerator.Generate. It then returnsUrlShortenResult.FromSuccess(...)withBaseUrl/{id}(or just{id}whenBaseUrlis empty).ExpandCoreAsync,GetStatisticsAsync, andUpdateAsyncreturn an error result withSHORTURL_EXPAND_FAILED/SHORTURL_GET_STATISTICS_FAILED/SHORTURL_UPDATE_FAILED. They require a storage-backed implementation.DeleteAsyncthrowsNotSupportedExceptionfor the same reason.TestConnectionAsyncalways returnstrue. 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 defaultShortUrlGeneratorusesRandomNumberGeneratorand an 8-character default length.
UrlShortenBuilder
SetLongUrl(string longUrl, bool enforceHttps = false)runs throughUriHelpers.GetValidWebUri(throwsInvalidFormatExceptionon invalid URLs). WhenenforceHttps, HTTP becomes HTTPS.SetCustomAlias(string? alias)must match^[a-zA-Z0-9\-]+$. Passingnull/whitespace clears the alias.SetExpirationDate(DateTime? date)must be in the future, ornull.Clear()resets the builder.Build()returns(LongUrl, CustomAlias, ExpirationDate). Throws ifLongUrlwas 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.Binder10.0.5(direct, microsoft)Microsoft.Extensions.DependencyInjection.Abstractions10.0.5(direct, microsoft)Microsoft.Extensions.Logging.Abstractions10.0.5(direct, microsoft)Microsoft.Extensions.Options10.0.5(direct, microsoft)Microsoft.Bcl.AsyncInterfaces10.0.5(transitive, microsoft, netstandard2.0)System.Memory4.6.3(transitive, microsoft, netstandard2.0)System.Text.Json10.0.5(transitive, microsoft, netstandard2.0)
| Product | Versions 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. |
-
.NETStandard 2.0
- Lyo.Common.Core (>= 3.0.0)
- Lyo.Exceptions (>= 3.0.0)
- Lyo.Metrics (>= 3.0.0)
- Lyo.Result (>= 3.0.0)
- Microsoft.Extensions.Configuration.Binder (>= 10.0.5)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.5)
- Microsoft.Extensions.Logging.Abstractions (>= 10.0.5)
- Microsoft.Extensions.Options (>= 10.0.5)
-
net10.0
- Lyo.Common.Core (>= 3.0.0)
- Lyo.Exceptions (>= 3.0.0)
- Lyo.Metrics (>= 3.0.0)
- Lyo.Result (>= 3.0.0)
- Microsoft.Extensions.Configuration.Binder (>= 10.0.5)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.5)
- Microsoft.Extensions.Logging.Abstractions (>= 10.0.5)
- Microsoft.Extensions.Options (>= 10.0.5)
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.