|
9 | 9 | using Microsoft.AspNetCore.Hosting;
|
10 | 10 | using Microsoft.AspNetCore.Http;
|
11 | 11 | using Microsoft.AspNetCore.Mvc.ApplicationModels;
|
| 12 | +using Microsoft.AspNetCore.Mvc.Razor.Compilation; |
12 | 13 | using Microsoft.AspNetCore.Server.Kestrel.Core;
|
13 | 14 | using Microsoft.Extensions.Configuration;
|
14 | 15 | using Microsoft.Extensions.DependencyInjection;
|
@@ -221,14 +222,42 @@ public static IUmbracoBuilder AddMvcAndRazor(this IUmbracoBuilder builder, Actio
|
221 | 222 | // We need to have runtime compilation of views when using umbraco. We could consider having only this when a specific config is set.
|
222 | 223 | // But as far as I can see, there are still precompiled views, even when this is activated, so maybe it is okay.
|
223 | 224 | IMvcBuilder mvcBuilder = builder.Services
|
224 |
| - .AddControllersWithViews() |
225 |
| - .AddRazorRuntimeCompilation(); |
| 225 | + .AddControllersWithViews(); |
| 226 | + |
| 227 | + FixForDotnet6Preview1(builder.Services); |
| 228 | + mvcBuilder.AddRazorRuntimeCompilation(); |
226 | 229 |
|
227 | 230 | mvcBuilding?.Invoke(mvcBuilder);
|
228 | 231 |
|
229 | 232 | return builder;
|
230 | 233 | }
|
231 | 234 |
|
| 235 | + /// <summary> |
| 236 | + /// This fixes an issue for .NET6 Preview1, that in AddRazorRuntimeCompilation cannot remove the existing IViewCompilerProvider. |
| 237 | + /// </summary> |
| 238 | + /// <remarks> |
| 239 | + /// When running .NET6 Preview1 there is an issue with looks to be fixed when running ASP.NET Core 6. |
| 240 | + /// This issue is because the default implementation of IViewCompilerProvider has changed, so the |
| 241 | + /// AddRazorRuntimeCompilation extension can't remove the default and replace with the runtimeviewcompiler. |
| 242 | + /// |
| 243 | + /// This method basically does the same as the ASP.NET Core 6 version of AddRazorRuntimeCompilation |
| 244 | + /// https://github.com/dotnet/aspnetcore/blob/f7dc5e24af7f9692a1db66741954b90b42d84c3a/src/Mvc/Mvc.Razor.RuntimeCompilation/src/DependencyInjection/RazorRuntimeCompilationMvcCoreBuilderExtensions.cs#L71-L80 |
| 245 | + /// |
| 246 | + /// While running .NET5 this does nothing as the ImplementationType has another FullName, and this is handled by the .NET5 version of AddRazorRuntimeCompilation |
| 247 | + /// </remarks> |
| 248 | + private static void FixForDotnet6Preview1(IServiceCollection services) |
| 249 | + { |
| 250 | + var compilerProvider = services.FirstOrDefault(f => |
| 251 | + f.ServiceType == typeof(IViewCompilerProvider) && |
| 252 | + f.ImplementationType?.Assembly == typeof(IViewCompilerProvider).Assembly && |
| 253 | + f.ImplementationType.FullName == "Microsoft.AspNetCore.Mvc.Razor.Compilation.DefaultViewCompiler"); |
| 254 | + |
| 255 | + if (compilerProvider != null) |
| 256 | + { |
| 257 | + services.Remove(compilerProvider); |
| 258 | + } |
| 259 | + } |
| 260 | + |
232 | 261 | /// <summary>
|
233 | 262 | /// Add runtime minifier support for Umbraco
|
234 | 263 | /// </summary>
|
|
0 commit comments