| | 1 | | <# |
| | 2 | | .SYNOPSIS |
| | 3 | | Writes an HTML response to the HTTP response body. |
| | 4 | | .DESCRIPTION |
| | 5 | | Serializes the provided HTML template with variables and writes it to the HTTP response. |
| | 6 | | .PARAMETER FilePath |
| | 7 | | The path to the HTML file to read and write to the response. This can be a relative or absolute path. |
| | 8 | | .PARAMETER Template |
| | 9 | | The HTML template string to write to the response. If provided, this will override the FilePath parameter. |
| | 10 | | .PARAMETER StatusCode |
| | 11 | | The HTTP status code to set for the response. Defaults to 200 (OK). |
| | 12 | | .PARAMETER Variables |
| | 13 | | A hashtable of variables to use for template placeholders. These will be merged into the HTML template. |
| | 14 | | .EXAMPLE |
| | 15 | | Write-KrHtmlResponse -FilePath "C:\path\to\template.html" -StatusCode 200 -Variables @{ Title = "My Page"; Conte |
| | 16 | | Reads the HTML file at "C:\path\to\template.html", merges in the variables, and writes the resulting HTML to the |
| | 17 | | .NOTES |
| | 18 | | This function is designed to be used in the context of a Kestrun server response. |
| | 19 | | #> |
| | 20 | | function Write-KrHtmlResponse { |
| | 21 | | [KestrunRuntimeApi('Route')] |
| | 22 | | [CmdletBinding(defaultParameterSetName = 'FilePath')] |
| | 23 | | [KestrunRuntimeApi('Route')] |
| | 24 | | [CmdletBinding()] |
| | 25 | | param( |
| | 26 | | [Parameter(Mandatory = $true, ParameterSetName = 'FilePath')] |
| | 27 | | [string]$FilePath, |
| | 28 | | [Parameter(Mandatory = $true, ParameterSetName = 'Template')] |
| | 29 | | [string]$Template, |
| | 30 | | [Parameter()] |
| | 31 | | [int]$StatusCode = 200, |
| | 32 | | [Parameter()] |
| | 33 | | [hashtable]$Variables |
| | 34 | | ) |
| | 35 | | try { |
| | 36 | | # Check if the Context.Response is available |
| 0 | 37 | | if ($null -ne $Context.Response) { |
| | 38 | |
|
| 0 | 39 | | $readOnlyDictionary = [Kestrun.Utilities.ReadOnlyDictionaryAdapter]::new($Variables) |
| | 40 | |
|
| 0 | 41 | | switch ($PSCmdlet.ParameterSetName) { |
| | 42 | | 'FilePath' { |
| | 43 | | # Resolve the file path relative to the Kestrun root if necessary |
| 0 | 44 | | $resolvedPath = Resolve-KrPath -Path $FilePath -KestrunRoot -Test |
| 0 | 45 | | Write-KrLog -Level Verbose -Message "Resolved file path: $resolvedPath" |
| | 46 | | # Call the C# method on the $Context.Response object |
| 0 | 47 | | $Context.Response.WriteHtmlResponseFromFile($resolvedPath, $readOnlyDictionary, $StatusCode) |
| 0 | 48 | | Write-Information "HTML response written for $FilePath" |
| | 49 | | } |
| | 50 | | 'Template' { |
| | 51 | | # Call the C# method on the $Context.Response object |
| 0 | 52 | | $Context.Response.WriteHtmlResponse($Template, $readOnlyDictionary, $StatusCode) |
| 0 | 53 | | Write-Information 'HTML response written from template' |
| | 54 | | } |
| | 55 | | } |
| | 56 | | } |
| | 57 | | } catch { |
| | 58 | | # Handle any errors that occur during the file response writing |
| 0 | 59 | | Write-KrLog -Level Error -Message 'Error writing file response.' -ErrorRecord $_ |
| | 60 | | } |
| | 61 | | } |
| | 62 | |
|