from ByteAether
Adds ASP.NET Core support for ByteAether.Ulid.
- Route constraints: register a
:ulidroute parameter policy that accepts only valid ULID values. - JSON support:
UlidandUlid?serialize as canonical 26-character strings without extra configuration in reflection-enabled applications. - OpenAPI support: expose ULIDs as strings with the
ulidformat in Swashbuckle and ASP.NET Core OpenAPI. - AOT-friendly: compatible with Native AOT; ASP.NET Core source-generated JSON metadata is required for application types.
For the core library and full details, see the GitHub repository.
dotnet add package ByteAether.Ulid.AspNetCoreThe package depends on ByteAether.Ulid. OpenAPI packages are optional and are not added transitively.
No extra packages are required for route constraints or JSON serialization.
using ByteAether.Ulid;
using ByteAether.Ulid.AspNetCore;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddUlidRouteConstraint();
var app = builder.Build();
app.MapGet("/users/{id:ulid}", (Ulid id) => Results.Ok(new UserResponse(id)));
app.Run();
public record UserResponse(Ulid Id);Behavior:
- route parameters must match a valid ULID, otherwise the route does not match
- Minimal API and MVC model binding accept valid ULID route and query values
- link generation accepts
Ulidvalues for:ulidroute parameters Ulidvalues in JSON bodies and responses use canonical 26-character strings; nullable values remain JSONnull
For Native AOT Minimal APIs, include Ulid and every request/response type in a source-generated JsonSerializerContext, then register that context:
using ByteAether.Ulid;
using System.Text.Json.Serialization;
builder.Services.ConfigureHttpJsonOptions(options =>
options.SerializerOptions.TypeInfoResolverChain.Insert(0, AppJsonSerializerContext.Default));
[JsonSerializable(typeof(Ulid))]
[JsonSerializable(typeof(UserResponse))]
internal partial class AppJsonSerializerContext : JsonSerializerContext
{
}Choose the OpenAPI integration you already use. Install only that package; this package does not add either dependency automatically.
dotnet add package Microsoft.AspNetCore.OpenApibuilder.Services.AddOpenApi(options => options.AddUlidSchemaTransformer());
app.MapOpenApi();For built-in OpenAPI, use the matching ASP.NET Core version:
- .NET 9:
Microsoft.AspNetCore.OpenApi9.0.0 or newer - .NET 10:
Microsoft.AspNetCore.OpenApi10.0.0 or newer
dotnet add package Swashbuckle.AspNetCorebuilder.Services.AddSwaggerGen(options => options.SchemaFilter<UlidSchemaFilter>());
app.UseSwagger();For Swashbuckle, use a version compatible with your target framework:
- .NET 6–9: 6.0.0 or newer
- .NET 10: 10.0.0 or newer
This project is licensed under the MIT License. See the LICENSE file for details.