| | 1 | | using System.Reflection; |
| | 2 | | using Kestrun.Razor; |
| | 3 | | using Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation; |
| | 4 | | using Microsoft.AspNetCore.Mvc.RazorPages; |
| | 5 | | using Microsoft.Extensions.FileProviders; |
| | 6 | | using Serilog.Events; |
| | 7 | |
|
| | 8 | | namespace Kestrun.Hosting; |
| | 9 | |
|
| | 10 | | /// <summary> |
| | 11 | | /// Provides extension methods for adding PowerShell and Razor Pages to a KestrunHost. |
| | 12 | | /// </summary> |
| | 13 | | public static class KestrunHostRazorExtensions |
| | 14 | | { |
| | 15 | | /// <summary> |
| | 16 | | /// Adds PowerShell Razor Pages to the application. |
| | 17 | | /// This middleware allows you to serve Razor Pages using PowerShell scripts. |
| | 18 | | /// </summary> |
| | 19 | | /// <param name="host">The KestrunHost instance to add Razor Pages to.</param> |
| | 20 | | /// <param name="routePrefix">The route prefix to use for the PowerShell Razor Pages.</param> |
| | 21 | | /// <param name="cfg">Configuration options for the Razor Pages.</param> |
| | 22 | | /// <returns>The current KestrunHost instance.</returns> |
| | 23 | | public static KestrunHost AddPowerShellRazorPages(this KestrunHost host, PathString? routePrefix, RazorPagesOptions? |
| | 24 | | { |
| 3 | 25 | | if (host.HostLogger.IsEnabled(LogEventLevel.Debug)) |
| | 26 | | { |
| 3 | 27 | | host.HostLogger.Debug("Adding PowerShell Razor Pages with route prefix: {RoutePrefix}, config: {@Config}", r |
| | 28 | | } |
| | 29 | |
|
| 3 | 30 | | return AddPowerShellRazorPages(host, routePrefix, dest => |
| 3 | 31 | | { |
| 0 | 32 | | if (cfg != null) |
| 3 | 33 | | { |
| 3 | 34 | | // simple value properties are fine |
| 0 | 35 | | dest.RootDirectory = cfg.RootDirectory; |
| 3 | 36 | |
|
| 3 | 37 | | // copy conventions one‑by‑one (collection is read‑only) |
| 0 | 38 | | foreach (var c in cfg.Conventions) |
| 3 | 39 | | { |
| 0 | 40 | | dest.Conventions.Add(c); |
| 3 | 41 | | } |
| 3 | 42 | | } |
| 3 | 43 | | }); |
| | 44 | | } |
| | 45 | |
|
| | 46 | | /// <summary> |
| | 47 | | /// Adds PowerShell Razor Pages to the application. |
| | 48 | | /// This middleware allows you to serve Razor Pages using PowerShell scripts. |
| | 49 | | /// </summary> |
| | 50 | | /// <param name="host">The KestrunHost instance to add Razor Pages to.</param> |
| | 51 | | /// <param name="routePrefix">The route prefix to use for the PowerShell Razor Pages.</param> |
| | 52 | | /// <returns>The current KestrunHost instance.</returns> |
| | 53 | | public static KestrunHost AddPowerShellRazorPages(this KestrunHost host, PathString? routePrefix) => |
| 1 | 54 | | AddPowerShellRazorPages(host: host, routePrefix: routePrefix, cfg: null as RazorPagesOptions); |
| | 55 | |
|
| | 56 | | /// <summary> |
| | 57 | | /// Adds PowerShell Razor Pages to the application with default configuration and no route prefix. |
| | 58 | | /// </summary> |
| | 59 | | /// <param name="host">The KestrunHost instance to add Razor Pages to.</param> |
| | 60 | | /// <returns>The current KestrunHost instance.</returns> |
| | 61 | | public static KestrunHost AddPowerShellRazorPages(this KestrunHost host) => |
| 1 | 62 | | AddPowerShellRazorPages(host: host, routePrefix: null, cfg: null as RazorPagesOptions); |
| | 63 | |
|
| | 64 | | // helper: true ⇢ file contains managed metadata |
| | 65 | | private static bool IsManaged(string path) |
| | 66 | | { |
| 0 | 67 | | try { _ = AssemblyName.GetAssemblyName(path); return true; } |
| 0 | 68 | | catch { return false; } // native ⇒ BadImageFormatException |
| 0 | 69 | | } |
| | 70 | | /// <summary> |
| | 71 | | /// Adds PowerShell Razor Pages to the application. |
| | 72 | | /// This middleware allows you to serve Razor Pages using PowerShell scripts. |
| | 73 | | /// </summary> |
| | 74 | | /// <param name="host">The KestrunHost instance to add Razor Pages to.</param> |
| | 75 | | /// <param name="routePrefix">The route prefix to use for the PowerShell Razor Pages.</param> |
| | 76 | | /// <param name="cfg">Configuration options for the Razor Pages.</param> |
| | 77 | | /// <returns>The current KestrunHost instance.</returns> |
| | 78 | | public static KestrunHost AddPowerShellRazorPages(this KestrunHost host, PathString? routePrefix, Action<RazorPagesO |
| | 79 | | { |
| 3 | 80 | | if (host.HostLogger.IsEnabled(LogEventLevel.Debug)) |
| | 81 | | { |
| 3 | 82 | | host.HostLogger.Debug("Adding PowerShell Razor Pages with route prefix: {RoutePrefix}, config: {@Config}", r |
| | 83 | | } |
| | 84 | |
|
| 3 | 85 | | _ = host.AddService(services => |
| 3 | 86 | | { |
| 0 | 87 | | var env = host.Builder.Environment; |
| 0 | 88 | | if (host.HostLogger.IsEnabled(LogEventLevel.Debug)) |
| 3 | 89 | | { |
| 0 | 90 | | host.HostLogger.Debug("Adding PowerShell Razor Pages to the service with route prefix: {RoutePrefix}", r |
| 3 | 91 | | } |
| 3 | 92 | |
|
| 0 | 93 | | _ = services.AddRazorPages().AddRazorRuntimeCompilation(); |
| 3 | 94 | |
|
| 3 | 95 | | // ── NEW: feed Roslyn every assembly already loaded ────────── |
| 3 | 96 | | // var env = builder.Environment; // or app.Environment |
| 0 | 97 | | var pagesRoot = Path.Combine(env.ContentRootPath, "Pages"); |
| 3 | 98 | |
|
| 0 | 99 | | _ = services.Configure<MvcRazorRuntimeCompilationOptions>(opts => |
| 0 | 100 | | { |
| 0 | 101 | | // 1️⃣ everything that’s already loaded and managed |
| 0 | 102 | | foreach (var asm in AppDomain.CurrentDomain.GetAssemblies() |
| 0 | 103 | | .Where(a => !a.IsDynamic && IsManaged(a.Location))) |
| 0 | 104 | | { |
| 0 | 105 | | opts.AdditionalReferencePaths.Add(asm.Location); |
| 0 | 106 | | } |
| 0 | 107 | |
|
| 0 | 108 | | // 2️⃣ managed DLLs from the .NET-8 shared-framework folder |
| 0 | 109 | | var coreDir = Path.GetDirectoryName(typeof(object).Assembly.Location)!; // e.g. …\dotnet\shared\Micros |
| 0 | 110 | | foreach (var dll in Directory.EnumerateFiles(coreDir, "*.dll") |
| 0 | 111 | | .Where(IsManaged)) |
| 0 | 112 | | { |
| 0 | 113 | | opts.AdditionalReferencePaths.Add(dll); |
| 0 | 114 | | } |
| 0 | 115 | |
|
| 0 | 116 | | // 3️⃣ (optional) watch your project’s Pages folder so edits hot-reload |
| 0 | 117 | | var pagesRoot = Path.Combine(host.Builder.Environment.ContentRootPath, "Pages"); |
| 0 | 118 | | if (Directory.Exists(pagesRoot)) |
| 0 | 119 | | { |
| 0 | 120 | | opts.FileProviders.Add(new PhysicalFileProvider(pagesRoot)); |
| 0 | 121 | | } |
| 0 | 122 | | }); |
| 3 | 123 | | }); |
| | 124 | |
|
| 3 | 125 | | return host.Use(app => |
| 3 | 126 | | { |
| 0 | 127 | | ArgumentNullException.ThrowIfNull(host.RunspacePool); |
| 0 | 128 | | if (host.HostLogger.IsEnabled(LogEventLevel.Debug)) |
| 3 | 129 | | { |
| 0 | 130 | | host.HostLogger.Debug("Adding PowerShell Razor Pages middleware with route prefix: {RoutePrefix}", route |
| 3 | 131 | | } |
| 3 | 132 | |
|
| 0 | 133 | | if (routePrefix.HasValue) |
| 3 | 134 | | { |
| 3 | 135 | | // ── /ps (or whatever prefix) ────────────────────────────── |
| 0 | 136 | | _ = app.Map(routePrefix.Value, branch => |
| 0 | 137 | | { |
| 0 | 138 | | _ = branch.UsePowerShellRazorPages(host.RunspacePool); // bridge |
| 0 | 139 | | _ = branch.UseRouting(); // add routing |
| 0 | 140 | | _ = branch.UseEndpoints(e => e.MapRazorPages()); // map pages |
| 0 | 141 | | }); |
| 3 | 142 | | } |
| 3 | 143 | | else |
| 3 | 144 | | { |
| 3 | 145 | | // ── mounted at root ──────────────────────────────────────── |
| 0 | 146 | | _ = app.UsePowerShellRazorPages(host.RunspacePool); // bridge |
| 0 | 147 | | _ = app.UseRouting(); // add routing |
| 0 | 148 | | _ = app.UseEndpoints(e => e.MapRazorPages()); // map pages |
| 3 | 149 | | } |
| 3 | 150 | |
|
| 0 | 151 | | if (host.HostLogger.IsEnabled(LogEventLevel.Debug)) |
| 3 | 152 | | { |
| 0 | 153 | | host.HostLogger.Debug("PowerShell Razor Pages middleware added with route prefix: {RoutePrefix}", routeP |
| 3 | 154 | | } |
| 3 | 155 | | }); |
| | 156 | | } |
| | 157 | |
|
| | 158 | |
|
| | 159 | | /// <summary> |
| | 160 | | /// Adds Razor Pages to the application. |
| | 161 | | /// </summary> |
| | 162 | | /// <param name="host">The KestrunHost instance to add Razor Pages to.</param> |
| | 163 | | /// <param name="cfg">The configuration options for Razor Pages.</param> |
| | 164 | | /// <returns>The current KestrunHost instance.</returns> |
| | 165 | | public static KestrunHost AddRazorPages(this KestrunHost host, RazorPagesOptions? cfg) |
| | 166 | | { |
| 1 | 167 | | if (host.HostLogger.IsEnabled(LogEventLevel.Debug)) |
| | 168 | | { |
| 1 | 169 | | host.HostLogger.Debug("Adding Razor Pages from source: {Source}", cfg); |
| | 170 | | } |
| | 171 | |
|
| 1 | 172 | | if (cfg == null) |
| | 173 | | { |
| 0 | 174 | | return host.AddRazorPages(); // no config, use defaults |
| | 175 | | } |
| | 176 | |
|
| 1 | 177 | | return host.AddRazorPages(dest => |
| 1 | 178 | | { |
| 1 | 179 | | // simple value properties are fine |
| 1 | 180 | | dest.RootDirectory = cfg.RootDirectory; |
| 1 | 181 | |
|
| 1 | 182 | | // copy conventions one‑by‑one (collection is read‑only) |
| 2 | 183 | | foreach (var c in cfg.Conventions) |
| 1 | 184 | | { |
| 0 | 185 | | dest.Conventions.Add(c); |
| 1 | 186 | | } |
| 2 | 187 | | }); |
| | 188 | | } |
| | 189 | |
|
| | 190 | | /// <summary> |
| | 191 | | /// Adds Razor Pages to the application. |
| | 192 | | /// This overload allows you to specify configuration options. |
| | 193 | | /// If you need to configure Razor Pages options, use the other overload. |
| | 194 | | /// </summary> |
| | 195 | | /// <param name="host">The KestrunHost instance to add Razor Pages to.</param> |
| | 196 | | /// <param name="cfg">The configuration options for Razor Pages.</param> |
| | 197 | | /// <returns>The current KestrunHost instance.</returns> |
| | 198 | | public static KestrunHost AddRazorPages(this KestrunHost host, Action<RazorPagesOptions>? cfg = null) |
| | 199 | | { |
| 3 | 200 | | if (host.HostLogger.IsEnabled(LogEventLevel.Debug)) |
| | 201 | | { |
| 3 | 202 | | host.HostLogger.Debug("Adding Razor Pages with configuration: {Config}", cfg); |
| | 203 | | } |
| | 204 | |
|
| 3 | 205 | | return host.AddService(services => |
| 3 | 206 | | { |
| 3 | 207 | | var mvc = services.AddRazorPages(); // returns IMvcBuilder |
| 3 | 208 | |
|
| 3 | 209 | | if (cfg != null) |
| 3 | 210 | | { |
| 2 | 211 | | _ = mvc.AddRazorPagesOptions(cfg); // ← the correct extension |
| 3 | 212 | | } |
| 3 | 213 | | // —OR— |
| 3 | 214 | | // services.Configure(cfg); // also works |
| 3 | 215 | | }) |
| 3 | 216 | | // optional: automatically map Razor endpoints after Build() |
| 6 | 217 | | .Use(app => ((IEndpointRouteBuilder)app).MapRazorPages()); |
| | 218 | | } |
| | 219 | | } |