ViewImports
Import common namespaces and enable tag helpers for all pages in the folder.
Full source
File: pwsh/tutorial/examples/11.1-RazorPages.ps1
<#
Sample Kestrun Server showing PowerShell-backed Razor Pages.
This example demonstrates enabling the Razor Pages runtime and serving pages from a root folder.
FileName: 11.1-RazorPages.ps1
#>
param(
[int]$Port = 5000,
[IPAddress]$IPAddress = [IPAddress]::Loopback
)
# Initialize Kestrun root directory
# the default value is $PWD
# This is recommended in order to use relative paths without issues
Initialize-KrRoot -Path $PSScriptRoot
# Create a new logger
New-KrLogger |
Set-KrLoggerLevel -Value Debug |
Add-KrSinkConsole |
Register-KrLogger -SetAsDefault -Name 'DefaultLogger'
# Create a new Kestrun server
New-KrServer -Name 'RazorPages'
# Add a listener on the configured port and IP address
Add-KrEndpoint -Port $Port -IPAddress $IPAddress
# Add a Razor Pages handler to the server
Add-KrPowerShellRazorPagesRuntime -RootPath './Assets/Pages'
# Application-wide metadata (AVAILABLE TO ALL RUNSPACES)
$AppInfo = [pscustomobject]@{
Name = 'Kestrun Razor Demo'
Environment = 'Development'
StartedUtc = [DateTime]::UtcNow
Version = Get-KrVersion -AsString
}
Write-KrLog -Level Information -Message "Starting Kestrun RazorPages server '{name}' version {version} in {environment} environment on {ipaddress}:{port}" `
-Values $AppInfo.Name, $AppInfo.Version, $AppInfo.Environment, $IPAddress, $Port
# Define feature flags for the application
$FeatureFlags = @{
RazorPages = $true
Cancellation = $true
HotReload = $false
}
Write-KrLog -Level Information -Message 'Feature Flags: {featureflags}' -Values $($FeatureFlags | ConvertTo-Json -Depth 3)
# Define a Message of the Day (MOTD) accessible to all pages
$Motd = @'
Welcome to Kestrun.
This message comes from the main server script.
Defined once, visible everywhere.
'@
Write-KrLog -Level Information -Message 'Message of the Day: {motd}' -Values $Motd
# Enable Kestrun configuration
Enable-KrConfiguration
# Start the server asynchronously
Start-KrServer
File: pwsh/tutorial/examples/Assets/Pages/_ViewImports.cshtml
@using System
@using System.Linq
@using Microsoft.AspNetCore.Http
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
Step-by-step
- Namespaces: Add
System/System.Linqso views can reference common types. - HTTP types: Import
Microsoft.AspNetCore.Http. - Tag helpers: Enable built-in MVC tag helpers (for example form helpers) with
@addTagHelper.
Try it
This file doesn’t have a direct URL, but it affects pages like /Contact.
curl -i http://127.0.0.1:5000/Contact
Troubleshooting
| Symptom | Cause | Fix |
|---|---|---|
| Razor compilation error for missing types | Namespace not imported | Add the required @using entry here or fully qualify the type |
| Tag helpers not recognized | Tag helpers not added | Ensure @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers is present |