| | | 1 | | <# |
| | | 2 | | .SYNOPSIS |
| | | 3 | | Writes a redirect response to the HTTP client. |
| | | 4 | | .DESCRIPTION |
| | | 5 | | Sets the Location header to the provided URL and optionally includes a |
| | | 6 | | message body describing the redirect. |
| | | 7 | | .PARAMETER Url |
| | | 8 | | The URL to redirect the client to. This should be a fully qualified URL. |
| | | 9 | | .PARAMETER Message |
| | | 10 | | An optional message to include in the response body. This can be used to provide additional context about the re |
| | | 11 | | .EXAMPLE |
| | | 12 | | Write-KrRedirectResponse -Url "https://example.com/new-page" -Message "You are being redirected to the new page. |
| | | 13 | | Redirects the client to "https://example.com/new-page" and includes a message in the response body. |
| | | 14 | | .NOTES |
| | | 15 | | This function is designed to be used in the context of a Kestrun server response. |
| | | 16 | | #> |
| | | 17 | | function Write-KrRedirectResponse { |
| | | 18 | | [KestrunRuntimeApi('Route')] |
| | | 19 | | [CmdletBinding()] |
| | | 20 | | param( |
| | | 21 | | [Parameter(Mandatory = $true)] |
| | | 22 | | [string]$Url, |
| | | 23 | | [Parameter()] |
| | | 24 | | [string]$Message |
| | | 25 | | ) |
| | | 26 | | # Only works inside a route script block where $Context is available |
| | 0 | 27 | | if ($null -ne $Context.Response) { |
| | | 28 | | # Call the C# method on the $Context.Response object |
| | 0 | 29 | | $Context.Response.WriteRedirectResponse($Url, $Message) |
| | | 30 | | } else { |
| | 0 | 31 | | Write-KrOutsideRouteWarning |
| | | 32 | | } |
| | | 33 | | } |
| | | 34 | | |