Index
Render the home page and show a model prepared by a sibling PowerShell script.
Full source
File: pwsh/tutorial/examples/Assets/Pages/Index.cshtml
@page
@model Kestrun.Razor.PwshKestrunModel
@{
ViewData["Title"] = "Home • Kestrun";
dynamic d = Model.Data ?? new { Welcome = "Hello!", Tagline = "No model", Tips = new string[0] };
}
<div class="card">
<h1>@d.Welcome</h1>
<p class="muted">@d.Tagline</p>
</div>
<div class="grid">
<div class="card">
<h2>What this demo shows</h2>
<ul>
<li>Razor renders HTML.</li>
<li>PowerShell prepares a model via <code>$Model</code>.</li>
<li>Model flows through <code>HttpContext.Items["PageModel"]</code>.</li>
</ul>
</div>
<div class="card">
<h2>Quick tips</h2>
@if (d.Tips != null && d.Tips.Length > 0)
{
<ul>
@foreach (var t in d.Tips)
{
<li>@t</li>
}
</ul>
}
else
{
<p class="muted">No tips available.</p>
}
</div>
</div>
File: pwsh/tutorial/examples/Assets/Pages/Index.cshtml.ps1
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '')]
param()
$Model = [pscustomobject]@{
Welcome = "Welcome to Kestrun ✨"
Tagline = "A tiny site rendered by Razor, powered by PowerShell models."
Tips = @(
"Open /Status to see request + server info.",
"Open /Quotes for a dynamic quote.",
"Open /Contact to test GET + POST.",
"Open /Cancel to test request abort cancellation."
)
}
Step-by-step
- View title: Set
ViewData["Title"]for the layout. - Model binding: Read
Model.Datainto a dynamic object (d). - Fallback: Use a default object when no model is available.
- Render: Show the welcome header and a tagline.
- Tips list: Render a list of tips when present.
Try it
curl -i http://127.0.0.1:5000/
Troubleshooting
| Symptom | Cause | Fix |
|---|---|---|
| Page renders “No model” | Index.cshtml.ps1 not found or not executed | Ensure the sample is started with Add-KrPowerShellRazorPagesRuntime -RootPath './Assets/Pages' |
| Layout not applied | _ViewStart.cshtml missing or layout path wrong | Confirm Assets/Pages/_ViewStart.cshtml sets Layout = "Shared/_Layout" |