Multi-language Routes (PS/C#/VB)
Map routes in PowerShell and implement others inline in C# or VB.NET.
Prerequisites: see Introduction.
Full source
File: pwsh/tutorial/examples/2.2-Multi-Language-Routes.ps1
<#
Sample Kestrun Server Configuration with Multiple Languages
This script demonstrates how to set up a simple Kestrun server with multiple routes and multiple languages.
Kestrun supports PowerShell, CSharp, and VBNet.
FileName: 2.2-Multi-Language-Routes.ps1
#>
param(
[int]$Port = 5000,
[IPAddress]$IPAddress = [IPAddress]::Loopback
)
# Create a new Kestrun server
New-KrServer -Name "Simple Server"
# Add a listener on the configured port and IP address
Add-KrEndpoint -Port $Port -IPAddress $IPAddress
# Add the PowerShell runtime
# Enable Kestrun configuration
Enable-KrConfiguration
# Map the route with PowerShell
Add-KrMapRoute -Verbs Get -Path "/hello-powershell" -ScriptBlock {
Write-KrTextResponse -InputObject "Hello, World!" -StatusCode 200
}
# Map the route with CSharp
Add-KrMapRoute -Verbs Get -Path "/hello-csharp" -Code @"
await Context.Response.WriteTextResponseAsync(inputObject: "Hello, World!", statusCode: 200);
// Or the synchronous version
// Context.Response.WriteTextResponse( "Hello, World!", 200);
"@ -Language CSharp
# Map the route with VBNet
Add-KrMapRoute -Verbs Get -Path "/hello-vbnet" -Code @"
Await Context.Response.WriteTextResponseAsync( "Hello, World!", 200)
' Or the synchronous version
' Context.Response.WriteTextResponse( "Hello, World!", 200);
"@ -Language VBNet
# Start the server asynchronously
Start-KrServer
Step-by-step
- Initialize root:
Initialize-KrRoot -Path $PSScriptRoot. - Server + listener:
New-KrServer;Add-KrEndpoint -Port 5000 -IPAddress Loopback. - Enable configuration:
Enable-KrConfiguration. - Map routes in multiple languages with distinct paths:
- PowerShell: GET
/hello-powershellwith-ScriptBlock { Write-KrTextResponse 'Hello from PS' }. - C#: GET
/hello-csharpwith-Language CSharp -Code @" ... @"; code can accessContext,Request,Responseand call helpers like WriteTextResponseAsync. - VB.NET: GET
/hello-vbnetwith-Language VBNet -Code @" ... @"following a similar pattern.
- PowerShell: GET
- Start the server:
Start-KrServer. - Route precedence: if multiple routes share the same verb and pattern, the last registered typically wins. Use different paths (e.g.,
/hello-ps,/hello-cs,/hello-vb) if you want to hit each explicitly.
Try it
Save the sample locally so it’s easy to run. Copy the contents of pwsh/tutorial/examples/2.2-Multi-Language-Routes.ps1 into a new file in an empty working folder (for example, multi-language.ps1), then run:
# From your working folder
pwsh .\multi-language.ps1
curl http://127.0.0.1:5000/hello-powershell
curl http://127.0.0.1:5000/hello-csharp
curl http://127.0.0.1:5000/hello-vbnet
PowerShell alternative:
Invoke-WebRequest -Uri 'http://127.0.0.1:5000/hello-powershell' | Select-Object -ExpandProperty Content
Invoke-WebRequest -Uri 'http://127.0.0.1:5000/hello-csharp' | Select-Object -ExpandProperty Content
Invoke-WebRequest -Uri 'http://127.0.0.1:5000/hello-vbnet' | Select-Object -ExpandProperty Content
Troubleshooting
- 404 Not Found: Make sure you’re calling one of the defined paths exactly:
/hello-powershell,/hello-csharp, or/hello-vbnet. - Port in use: Change
-PortinAdd-KrEndpointor stop the conflicting process. - Binding issues: Use
-IPAddress Loopbackfor local-only testing orAnyfor LAN access. - Nothing prints: Ensure you ran
Enable-KrConfigurationbeforeStart-KrServer.
References
Previous / Next
Go back to Multiple Content Types or continue to Route Parameters.