Skip to content

ASP.NET Core

github-actions[bot] edited this page Nov 29, 2026 · 1 revision

ASP.NET Core Integration (Celly.Json.AspNetCore)

Celly.Json.AspNetCore integrates Celly.Json into ASP.NET Core with zero third-party dependencies beyond Celly.Json and Microsoft.AspNetCore.App (fully NativeAOT and trimming compatible).

Installation

dotnet add package Celly.Json.AspNetCore

Service Registration

Register CelEnv, CelJsonOptions, ICelProgramCache, and CelAuthorizationHandler in Program.cs:

builder.Services.AddCellyJson(options =>
{
    options.MaxCachedPrograms = 1024;
    options.MaxJsonBodyBytes = 1024 * 1024; // 1 MiB cap when buffering JSON bodies
});

Lazy HttpContextActivation

Expressions evaluated in authorization policies and endpoint filters receive a lazy HttpContextActivation with three root variables:

Root Variable Fields / Indexing
request method, path, scheme, host, contentType, headers['X-Tenant'], query['page'], route['id']
user authenticated, name, authenticationType, roles (supports 'admin' in user.roles), claims['dept']
body Lazily adapted JSON request body (when parseJsonBody: true; automatically buffered and rewound so downstream handlers can still read it)

CEL Authorization Policies (AddCelPolicy)

builder.Services.AddAuthorizationBuilder()
    .AddCelPolicy(
        "CanRefund",
        "user.authenticated && ('admin' in user.roles || (body.amount < 100 && user.claims['dept'] == 'support'))",
        parseJsonBody: true);

app.MapPost("/refunds", HandleRefund).RequireAuthorization("CanRefund");

Minimal API Endpoint Filters (.RequireCel(...))

Guard any endpoint or route group with a boolean CEL expression:

app.MapPost("/orders", CreateOrder)
   .RequireCel(
       "body.total > 0 && body.items.all(i, i.qty > 0)",
       failureStatusCode: StatusCodes.Status400BadRequest,
       failureMessage: "Order total and item quantities must be positive.");

Zero-Copy CelValue JSON Responses (Results.Extensions.CelJson)

Stream any CelValue directly to HttpResponse.BodyWriter via Utf8JsonWriter without allocating intermediate strings or byte arrays:

app.MapPost("/eval", (CelProgram program, JsonDocument payload) =>
{
    var result = program.EvalJson(payload);
    return Results.Extensions.CelJson(result);
});

Clone this wiki locally