Path: redb-route/redb.Route.Templates/TemplateModel.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route.Templates/TemplateModel.cs
----
using System.Collections;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Xml.Linq;
using redb.Route.Abstractions;
using redb.Route.Core;
using redb.Route.Expressions;
using Scriban.Runtime;
namespace redb.Route.Templates;
///
/// The data a template sees — one model for the DSL and the XML form:
/// body (JSON text parsed to an object tree, XML text to a navigable tree, a POCO as-is with
/// its public members), headers.name / headers["Content-Type"], properties.name,
/// exception.message, args.name, and two functions: expr("header.a * 2")
/// (the route expression language, same engine as ${...}) and raw (skip escaping).
/// No exchange, no context, no methods (README decision 4).
///
internal static class TemplateModel
{
public static ScriptObject Build(IExchange exchange, IReadOnlyDictionary args)
{
var model = new ScriptObject
{
["body"] = ConvertBody(exchange.In.Body, exchange.In.ContentType),
["headers"] = ToScriptObject(exchange.In.Headers),
// Framework bookkeeping (__redb_scope:* holds live DI scopes) is not data; same rule as JsonTransform.
["properties"] = ToScriptObject(exchange.Properties.Where(p => !p.Key.StartsWith("__", StringComparison.Ordinal))),
["exception"] = exchange.Exception is { } ex
? new ScriptObject { ["message"] = ex.Message, ["type"] = ex.GetType().Name, ["stackTrace"] = ex.StackTrace }
: null,
["args"] = ToScriptObject(args),
};
model.Import("expr", new Func(expression => ExpressionResolver.ResolveExpression(expression, exchange)));
model.Import("raw", new Func