| | 1 | | <# |
| | 2 | | .SYNOPSIS |
| | 3 | | Adds a new HTML template route to the Kestrun server. |
| | 4 | |
|
| | 5 | | .DESCRIPTION |
| | 6 | | This function allows you to add a new HTML template route to the Kestrun server by specifying the route path and |
| | 7 | |
|
| | 8 | | .PARAMETER Server |
| | 9 | | The Kestrun server instance to which the route will be added. |
| | 10 | | If not specified, the function will attempt to resolve the current server context. |
| | 11 | |
|
| | 12 | | .PARAMETER Pattern |
| | 13 | | The URL path for the new route. |
| | 14 | |
|
| | 15 | | .PARAMETER HtmlTemplatePath |
| | 16 | | The file path to the HTML template to be used for the route. |
| | 17 | |
|
| | 18 | | .PARAMETER Authorization |
| | 19 | | An optional array of authorization strings for the route. |
| | 20 | |
|
| | 21 | | .PARAMETER PassThru |
| | 22 | | If specified, the function will return the created route object. |
| | 23 | |
|
| | 24 | | .OUTPUTS |
| | 25 | | [Microsoft.AspNetCore.Builder.IEndpointConventionBuilder] representing the created route. |
| | 26 | |
|
| | 27 | | .EXAMPLE |
| | 28 | | Add-KrHtmlTemplateRoute -Server $myServer -Path "/myroute" -HtmlTemplatePath "C:\Templates\mytemplate.html" |
| | 29 | | Adds a new HTML template route to the specified Kestrun server with the given path and template file. |
| | 30 | |
|
| | 31 | | .EXAMPLE |
| | 32 | | Get-KrServer | Add-KrHtmlTemplateRoute -Path "/myroute" -HtmlTemplatePath "C:\Templates\mytemplate.html" -PassTh |
| | 33 | | Adds a new HTML template route to the current Kestrun server and returns the route object |
| | 34 | | .NOTES |
| | 35 | | This function is part of the Kestrun PowerShell module and is used to manage routes |
| | 36 | | #> |
| | 37 | | function Add-KrHtmlTemplateRoute { |
| | 38 | | [KestrunRuntimeApi('Definition')] |
| | 39 | | [CmdletBinding()] |
| | 40 | | [OutputType([Kestrun.Hosting.KestrunHost])] |
| | 41 | | param( |
| | 42 | | [Parameter(Mandatory = $false, ValueFromPipeline = $true)] |
| | 43 | | [Kestrun.Hosting.KestrunHost]$Server, |
| | 44 | |
|
| | 45 | | [Parameter(Mandatory = $true)] |
| | 46 | | [alias('Path')] |
| | 47 | | [string]$Pattern, |
| | 48 | |
|
| | 49 | | [Parameter(Mandatory = $true)] |
| | 50 | | [string]$HtmlTemplatePath, |
| | 51 | |
|
| | 52 | | [Parameter()] |
| | 53 | | [string[]]$Authorization = $null, |
| | 54 | |
|
| | 55 | | [Parameter()] |
| | 56 | | [switch]$PassThru |
| | 57 | | ) |
| | 58 | | begin { |
| | 59 | | # Ensure the server instance is resolved |
| 0 | 60 | | $Server = Resolve-KestrunServer -Server $Server |
| 0 | 61 | | if ($null -eq $Server) { |
| 0 | 62 | | throw 'Server is not initialized. Please ensure the server is configured before setting options.' |
| | 63 | | } |
| | 64 | | } |
| | 65 | | process { |
| | 66 | |
|
| 0 | 67 | | $options = [Kestrun.Hosting.Options.MapRouteOptions]::new() |
| 0 | 68 | | $options.HttpVerbs = [Kestrun.Utilities.HttpVerb[]]::new([Kestrun.Utilities.HttpVerb]::Get) |
| 0 | 69 | | $options.Pattern = $Pattern |
| 0 | 70 | | $options.RequireAuthorization = $Authorization |
| | 71 | |
|
| 0 | 72 | | [Kestrun.Hosting.KestrunHostMapExtensions]::AddHtmlTemplateRoute($Server, $options, $HtmlTemplatePath) | Out-Nul |
| 0 | 73 | | if ($PassThru) { |
| 0 | 74 | | return $Server |
| | 75 | | } |
| | 76 | | } |
| | 77 | | } |
| | 78 | |
|