| | | 1 | | <# |
| | | 2 | | .SYNOPSIS |
| | | 3 | | Sets only the HTTP status code for the response, without a body. |
| | | 4 | | .DESCRIPTION |
| | | 5 | | Sets the HTTP status code for the response and clears any body or content type, |
| | | 6 | | allowing status code pages middleware to handle the response body if configured. |
| | | 7 | | .PARAMETER StatusCode |
| | | 8 | | The HTTP status code to set for the response. |
| | | 9 | | .EXAMPLE |
| | | 10 | | Write-KrStatusResponse -StatusCode 404 |
| | | 11 | | Sets the response status code to 404 Not Found, without a body. If status code pages |
| | | 12 | | middleware is enabled, it will generate the response body. |
| | | 13 | | .NOTES |
| | | 14 | | This function is designed to be used in the context of a Kestrun server response. |
| | | 15 | | #> |
| | | 16 | | function Write-KrStatusResponse { |
| | | 17 | | [KestrunRuntimeApi('Route')] |
| | | 18 | | [CmdletBinding()] |
| | | 19 | | param( |
| | | 20 | | [Parameter(Mandatory = $true)] |
| | | 21 | | [int]$StatusCode |
| | | 22 | | ) |
| | | 23 | | |
| | | 24 | | # Only works inside a route script block where $Context is available |
| | 0 | 25 | | if ($null -eq $Context -or $null -eq $Context.Response) { |
| | 0 | 26 | | Write-KrOutsideRouteWarning |
| | | 27 | | return |
| | | 28 | | } |
| | | 29 | | |
| | | 30 | | # Write only the status code, letting any status code pages middleware handle the response body |
| | 0 | 31 | | $Context.Response.WriteStatusOnly($StatusCode) |
| | | 32 | | } |
| | | 33 | | |