Table of Contents

LikeC4 in Scalar

Bielu.AspNetCore.LikeC4.Scalar draws the views of a LikeC4 architecture model in the Scalar API Reference, on the same page as the API documents the model describes. It embeds the @bielu/scalar-likec4 plugin and serves it from the app. To see what each view looks like in the reference, see What it shows.

The asyncapiChannels view of a shop in Scalar: the views in the sidebar, Orders API sending OrderCreated to the orders.created queue, which delivers it on

Important

The plugin uses Scalar's plugin documentTypes API, first released in @scalar/api-reference 1.80.0 (scalar/scalar#10816). Scalar.AspNetCore bundles that version from 3.0.1 on, and this package depends on it.

Setup

dotnet add package Bielu.AspNetCore.LikeC4.Scalar
npx likec4 export json -o wwwroot/architecture.likec4.json architecture

The model is the JSON likec4 export json writes. That file is already laid out, so the plugin draws it as it is and needs neither LikeC4 nor Graphviz in the browser. A model generated from AsyncAPI documents with dotnet asyncapi likec4 (LikeC4 architecture models) is exported the same way.

app.MapStaticAssets();                                          // serves wwwroot/architecture.likec4.json
app.MapScalarLikeC4Assets();                                    // serves the bundle at /bielu/scalar/likec4/plugin.js

app.MapScalarApiReference(options => options
    .AddAsyncApiDocument("v1", "Events")
    .AddLikeC4Document("architecture", "Architecture")          // /{documentName}.likec4.json by default
    .WithLikeC4());

The app serves the model itself: the package adds no endpoint for it.

From a custom path

The model at another route. routePattern is the URL Scalar fetches the model from. {documentName} in it is replaced with the model's name, and a pattern without the placeholder is used as it is:

options
    .AddLikeC4Document("architecture", "Architecture", routePattern: "/docs/{documentName}.json")   // /docs/architecture.json
    .AddLikeC4Document("payments", "Payments", routePattern: "/models/payments-c4.json");          // a fixed URL

The model in a folder outside wwwroot. Serve that folder at the route the pattern names, for example with static files:

using Microsoft.Extensions.FileProviders;

// architecture/architecture.json, in the project folder, served at /docs/architecture.json
app.UseStaticFiles(new StaticFileOptions
{
    FileProvider = new PhysicalFileProvider(Path.Combine(app.Environment.ContentRootPath, "architecture")),
    RequestPath = "/docs",
});

Any endpoint that returns the JSON works as well, for example a MapGet that reads the model from storage, as long as its URL matches routePattern.

The plugin bundle at another path. MapScalarLikeC4Assets and WithLikeC4 take the same base path, so give both the same value:

app.MapScalarLikeC4Assets("/assets/likec4");                    // serves /assets/likec4/plugin.js

app.MapScalarApiReference(options => options
    .AddLikeC4Document("architecture", "Architecture", routePattern: "/docs/{documentName}.json")
    .WithLikeC4("/assets/likec4"));

.NET Aspire

For the Scalar resource of a .NET Aspire AppHost, Bielu.AspNetCore.LikeC4.Scalar.Aspire registers the plugin through Scalar's PluginUrls option. By default it loads dist/scalar-plugin.mjs from jsDelivr, pinned to the npm version released with the package:

dotnet add package Bielu.AspNetCore.LikeC4.Scalar.Aspire
var api = builder.AddProject<Projects.MyApi>("api");   // serves /architecture.likec4.json as a static file

builder.AddScalarApiReference()
    .WithLikeC4()
    .WithApiReference(api, options => options
        .AddDocument("v1")
        .AddLikeC4Document("architecture", "Architecture"));

routePattern: on AddLikeC4Document works as above, against the API project that serves the model. WithLikeC4(pluginUrl:) loads the plugin module from any URL instead of jsDelivr, for example a copy you host where the CDN is unreachable:

builder.AddScalarApiReference()
    .WithLikeC4(pluginUrl: "https://assets.example.com/scalar-likec4/scalar-plugin.mjs")
    .WithApiReference(api, options => options
        .AddLikeC4Document("architecture", "Architecture", routePattern: "/docs/{documentName}.json"));

The Scalar container keeps its own bundle and only the plugin is added, so it combines with the Arazzo document type and the SignalR and gRPC consoles on the same resource. The container image must bundle @scalar/api-reference 1.80.0 or later.

Without ASP.NET Core

To use the plugin from npm, a <script> tag or Scalar's pluginUrls, see LikeC4 for Scalar.