using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.ViewEngines; using Microsoft.Extensions.Options; using Umbraco.Cms.Core.Logging; namespace Umbraco.Cms.Web.Website.ViewEngines; /// /// Wraps all view engines with a /// public class ProfilingViewEngineWrapperMvcViewOptionsSetup : IConfigureOptions { private readonly IProfiler _profiler; /// /// Initializes a new instance of the class. /// /// The public ProfilingViewEngineWrapperMvcViewOptionsSetup(IProfiler profiler) => _profiler = profiler ?? throw new ArgumentNullException(nameof(profiler)); /// public void Configure(MvcViewOptions options) { if (options == null) { throw new ArgumentNullException(nameof(options)); } WrapViewEngines(options.ViewEngines); } private void WrapViewEngines(IList viewEngines) { if (viewEngines.Count == 0) { return; } var originalEngines = viewEngines.ToList(); viewEngines.Clear(); foreach (IViewEngine engine in originalEngines) { IViewEngine wrappedEngine = engine is ProfilingViewEngine ? engine : new ProfilingViewEngine(engine, _profiler); viewEngines.Add(wrappedEngine); } } }