| | 1 | | <# |
| | 2 | | .SYNOPSIS |
| | 3 | | Adds Razor Pages service to the server. |
| | 4 | | .DESCRIPTION |
| | 5 | | This cmdlet allows you to register Razor Pages with the Kestrun server. |
| | 6 | | It can be used to serve dynamic web pages using Razor syntax. |
| | 7 | | .PARAMETER Server |
| | 8 | | The Kestrun server instance to which the Razor Pages service will be added. |
| | 9 | | .PARAMETER Options |
| | 10 | | The RazorPagesOptions to configure the Razor Pages service. |
| | 11 | | .PARAMETER RootDirectory |
| | 12 | | The root directory for the Razor Pages. |
| | 13 | | .PARAMETER Conventions |
| | 14 | | An array of page conventions to apply to the Razor Pages. |
| | 15 | | .PARAMETER PassThru |
| | 16 | | If specified, the cmdlet will return the modified server instance. |
| | 17 | | .EXAMPLE |
| | 18 | | $server | Add-KrRazorPageService -RootDirectory '/Pages' -Conventions $conventions |
| | 19 | | This example adds Razor Pages service to the server, specifying the root directory and conventions for the pages |
| | 20 | | .EXAMPLE |
| | 21 | | $server | Add-KrRazorPageService -Options $options |
| | 22 | | This example adds Razor Pages service to the server using the specified RazorPagesOptions. |
| | 23 | | .LINK |
| | 24 | | https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.razorpages.razorpagesoptions?view=aspnetco |
| | 25 | | .NOTES |
| | 26 | | This cmdlet is used to register Razor Pages with the Kestrun server, allowing you to serve dynamic web pages usi |
| | 27 | | #> |
| | 28 | | function Add-KrRazorPageService { |
| | 29 | | [KestrunRuntimeApi('Definition')] |
| | 30 | | [CmdletBinding(defaultParameterSetName = 'Items')] |
| | 31 | | [OutputType([Kestrun.Hosting.KestrunHost])] |
| | 32 | | param( |
| | 33 | | [Parameter(Mandatory = $false, ValueFromPipeline = $true)] |
| | 34 | | [Kestrun.Hosting.KestrunHost]$Server, |
| | 35 | |
|
| | 36 | | [Parameter(Mandatory = $true, ParameterSetName = 'Options')] |
| | 37 | | [Microsoft.AspNetCore.Mvc.RazorPages.RazorPagesOptions]$Options, |
| | 38 | |
|
| | 39 | | [Parameter(ParameterSetName = 'Items')] |
| | 40 | | [string]$RootDirectory, |
| | 41 | |
|
| | 42 | | [Parameter(ParameterSetName = 'Items')] |
| | 43 | | [Microsoft.AspNetCore.Mvc.ApplicationModels.IPageConvention[]]$Conventions = @(), |
| | 44 | |
|
| | 45 | | [Parameter()] |
| | 46 | | [switch]$PassThru |
| | 47 | | ) |
| | 48 | | begin { |
| | 49 | | # Ensure the server instance is resolved |
| 0 | 50 | | $Server = Resolve-KestrunServer -Server $Server |
| 0 | 51 | | if ($null -eq $Server) { |
| 0 | 52 | | throw 'Server is not initialized. Please ensure the server is configured before setting options.' |
| | 53 | | } |
| | 54 | | } |
| | 55 | | process { |
| 0 | 56 | | if ($PSCmdlet.ParameterSetName -eq 'Items') { |
| 0 | 57 | | $Options = [Microsoft.AspNetCore.Mvc.RazorPages.RazorPagesOptions]::new() |
| 0 | 58 | | if (-not [string]::IsNullOrWhiteSpace($RootDirectory)) { |
| 0 | 59 | | $Options.RootDirectory = $RootDirectory |
| | 60 | | } |
| 0 | 61 | | if ($Conventions.Count -gt 0) { |
| 0 | 62 | | foreach ($c in $Conventions) { |
| 0 | 63 | | $Options.Conventions.Add($c) |
| | 64 | | } |
| | 65 | | } |
| | 66 | | } |
| | 67 | |
|
| 0 | 68 | | [Kestrun.Hosting.KestrunHostRazorExtensions]::AddRazorPages($Server, $Options) | Out-Null |
| | 69 | |
|
| 0 | 70 | | if ($PassThru.IsPresent) { |
| | 71 | | # if the PassThru switch is specified, return the server instance |
| | 72 | | # Return the modified server instance |
| 0 | 73 | | return $Server |
| | 74 | | } |
| | 75 | | } |
| | 76 | | } |
| | 77 | |
|