AppInfo

Display application-wide variables defined in the main server script and shared across runspaces.

Full source

File: pwsh/tutorial/examples/Assets/Pages/AppInfo.cshtml

@page
@model Kestrun.Razor.PwshKestrunModel
@{
    ViewData["Title"] = "Application Info";

     dynamic d = Model.Data ?? new {
        App = (object)null,
        Flags = (IDictionary<string, object>)new Dictionary<string, object>(),
        Motd = ""
    };

}

<div class="card">
    <h1>Application Info</h1>
    <p class="muted">Values defined in the main server script.</p>
</div>

@if (d.App != null)
{
    <div class="card">
        <h2>App</h2>
        <ul>
            <li><strong>Name:</strong> @d.App.Name</li>
            <li><strong>Environment:</strong> @d.App.Environment</li>
            <li><strong>Version:</strong> @d.App.Version</li>
            <li><strong>Started (UTC):</strong> @d.App.StartedUtc</li>
        </ul>
    </div>
}

@if (d.Flags != null)
{
    <div class="card">
        <h2>Feature Flags</h2>
        <ul>
            @foreach (var k in d.Flags.Keys)
            {
                <li><strong>@k</strong>: @d.Flags[k]</li>
            }
        </ul>
    </div>
}

@if (!string.IsNullOrEmpty(d.Motd))
{
    <div class="card">
        <h2>Message of the Day</h2>
        <pre>@d.Motd</pre>
    </div>
}


@if (d.Host != null)
{
    <div class="card">
        <h2>Host</h2>
        <pre>@d.Host</pre>
    </div>
}

File: pwsh/tutorial/examples/Assets/Pages/AppInfo.cshtml.ps1

[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '')]
param()
# These variables EXIST because they were defined
# before Enable-KrConfiguration in the main script

$Model = [pscustomobject]@{
    App = $AppInfo
    Flags = $FeatureFlags
    Motd = $Motd
    Host = (Expand-KrObject -InputObject $KrServer -PassThru)
}

Step-by-step

  1. Shared state: Define $AppInfo, $FeatureFlags, and $Motd in the main server script before Enable-KrConfiguration.
  2. Model: Read those variables in AppInfo.cshtml.ps1.
  3. Host dump: Use Expand-KrObject to create a readable snapshot of $KrServer.
  4. View: Render App details, feature flags, MOTD, and the host dump.

Try it

curl -i http://127.0.0.1:5000/AppInfo

Troubleshooting

Symptom Cause Fix
App section missing $AppInfo not set or not in scope Ensure $AppInfo is defined in the server script before Enable-KrConfiguration
Flags are empty $FeatureFlags not set Define $FeatureFlags in the server script and keep it in scope
Host dump fails Expand-KrObject unavailable Ensure the Kestrun module is imported and available to the Razor runspaces

References


Previous / Next

Previous: Contact Next: Cancel