Quotes
Generate a “quote of the moment” in PowerShell and render it with Razor.
Full source
File: pwsh/tutorial/examples/Assets/Pages/Quotes.cshtml
@page
@model Kestrun.Razor.PwshKestrunModel
@{
ViewData["Title"] = "Quotes • Kestrun";
dynamic d = Model.Data ?? new { Quote = "No quote", Author = "", Seed = "" };
}
<div class="card">
<h1>Quote of the moment</h1>
<blockquote style="margin:0; font-size:1.2rem;">
“@d.Quote”
<div class="muted" style="margin-top:0.6rem;">— @d.Author</div>
</blockquote>
</div>
<div class="card">
<p class="muted">Refresh to get a new one. Seed: <code>@d.Seed</code></p>
</div>
File: pwsh/tutorial/examples/Assets/Pages/Quotes.cshtml.ps1
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '')]
param()
$quotes = @(
@{ Quote="Simplicity is prerequisite for reliability."; Author="Edsger W. Dijkstra" },
@{ Quote="Make it work, make it right, make it fast."; Author="Kent Beck" },
@{ Quote="Programs must be written for people to read."; Author="Harold Abelson" },
@{ Quote="Premature optimization is the root of all evil."; Author="Donald Knuth" }
)
$seed = [Guid]::NewGuid().ToString("N").Substring(0,8)
$rand = New-Object System.Random
$pick = $quotes[$rand.Next(0, $quotes.Count)]
$Model = [pscustomobject]@{
Quote = $pick.Quote
Author = $pick.Author
Seed = $seed
}
Step-by-step
- Quote list: Define a small in-memory list of quote/author pairs.
- Seed: Create a short GUID-based seed for display.
- Pick: Choose one quote at random.
- Model: Set
$ModelwithQuote,Author, andSeed. - Render: Display the quote and encourage refresh to get a new one.
Try it
curl -i http://127.0.0.1:5000/Quotes
Troubleshooting
| Symptom | Cause | Fix |
|---|---|---|
| Same quote repeats | Random selection by chance | Refresh a few times or expand the $quotes list |
| Razor shows “No quote” | Model not set | Ensure Quotes.cshtml.ps1 executes and assigns $Model |