Status

Display request and runtime details by reading $Context and passing a formatted model to Razor.

Full source

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

@page
@model Kestrun.Razor.PwshKestrunModel
@{
    ViewData["Title"] = "Status • Kestrun";
    dynamic d = Model.Data ?? new { NowUtc = "", Method = "", Path = "", RemoteIp = "", Headers = "" };
}

<div class="card">
    <h1>Status</h1>
    <p class="muted">A quick peek at request + runtime data.</p>
</div>

<div class="grid">
    <div class="card">
        <h2>Request</h2>
        <ul>
            <li><strong>UTC:</strong> @d.NowUtc</li>
            <li><strong>Method:</strong> @d.Method</li>
            <li><strong>Path:</strong> @d.Path</li>
            <li><strong>Remote IP:</strong> @d.RemoteIp</li>
        </ul>
    </div>
    <div class="card">
        <h2>Headers (subset)</h2>
        <pre>@d.Headers</pre>
    </div>
</div>

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

[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '')]
param()

$req = $Context.Request
$ip = $Context.Connection.RemoteIpAddress

# Show a small, readable subset
$keys = @('User-Agent', 'Accept', 'Accept-Language', 'Host')
$lines = foreach ($k in $keys) {
    $v = $req.Headers[$k]
    if ($v.Count -gt 0) { "$($k): $($v -join ', ')" }
}
$headersText = ($lines -join "`n")

$Model = [pscustomobject]@{
    NowUtc = [DateTime]::UtcNow.ToString('u')
    Method = $req.Method
    Path = $req.Path.Value
    RemoteIp = if ($ip) { $ip.ToString() } else { '' }
    Headers = $headersText
}

Step-by-step

  1. Request: Read $Context.Request and the remote IP from $Context.Connection.
  2. Header subset: Build a small, readable header list.
  3. Model: Populate $Model with method, path, UTC time, remote IP, and header text.
  4. View title: Set ViewData["Title"].
  5. Render: Show request details and a header dump.

Try it

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

Troubleshooting

Symptom Cause Fix
Remote IP is blank Loopback or proxy hides address This is expected on local runs; configure forwarded headers if behind a proxy
Headers section empty Header keys not present Update $keys in the script to include the headers you’re sending

References


Previous / Next

Previous: About Next: Quotes