// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; using System.Diagnostics.Tracing; using System.Threading.Tasks; using Microsoft.AspNet.Mvc.Infrastructure; using Microsoft.AspNet.Mvc.ViewEngines; using Microsoft.Extensions.Logging; using Microsoft.Extensions.OptionsModel; namespace Microsoft.AspNet.Mvc.ViewFeatures { /// /// Finds and executes an for a . /// public class ViewResultExecutor : ViewExecutor { #pragma warning disable 0618 /// /// Creates a new . /// /// The . /// The . /// The . /// The . /// The . public ViewResultExecutor( IOptions viewOptions, IHttpResponseStreamWriterFactory writerFactory, ICompositeViewEngine viewEngine, TelemetrySource telemetry, ILoggerFactory loggerFactory) : base(viewOptions, writerFactory, viewEngine, telemetry) { if (loggerFactory == null) { throw new ArgumentNullException(nameof(loggerFactory)); } Logger = loggerFactory.CreateLogger(); } #pragma warning restore 0618 /// /// Gets the . /// protected ILogger Logger { get; } /// /// Attempts to find the associated with . /// /// The associated with the current request. /// The . /// A . public virtual ViewEngineResult FindView(ActionContext actionContext, ViewResult viewResult) { if (actionContext == null) { throw new ArgumentNullException(nameof(actionContext)); } if (viewResult == null) { throw new ArgumentNullException(nameof(viewResult)); } var viewEngine = viewResult.ViewEngine ?? ViewEngine; var viewName = viewResult.ViewName ?? actionContext.ActionDescriptor.Name; var result = viewEngine.FindView(actionContext, viewName); if (result.Success) { #pragma warning disable 0618 if (Telemetry.IsEnabled("Microsoft.AspNet.Mvc.ViewFound")) { Telemetry.WriteTelemetry( "Microsoft.AspNet.Mvc.ViewFound", new { actionContext = actionContext, isPartial = false, result = viewResult, viewName = viewName, view = result.View, }); } #pragma warning restore 0618 Logger.LogVerbose("The view '{ViewName}' was found.", viewName); } else { #pragma warning disable 0618 if (Telemetry.IsEnabled("Microsoft.AspNet.Mvc.ViewNotFound")) { Telemetry.WriteTelemetry( "Microsoft.AspNet.Mvc.ViewNotFound", new { actionContext = actionContext, isPartial = false, result = viewResult, viewName = viewName, searchedLocations = result.SearchedLocations }); } #pragma warning restore 0618 Logger.LogError( "The view '{ViewName}' was not found. Searched locations: {SearchedViewLocations}", viewName, result.SearchedLocations); } return result; } /// /// Executes the asynchronously. /// /// The associated with the current request. /// The . /// The . /// A which will complete when view execution is completed. public virtual Task ExecuteAsync(ActionContext actionContext, IView view, ViewResult viewResult) { if (actionContext == null) { throw new ArgumentNullException(nameof(actionContext)); } if (view == null) { throw new ArgumentNullException(nameof(view)); } if (viewResult == null) { throw new ArgumentNullException(nameof(viewResult)); } return ExecuteAsync( actionContext, view, viewResult.ViewData, viewResult.TempData, viewResult.ContentType, viewResult.StatusCode); } } }