Esempio n. 1
0
 private void OutputDiagnostics(ActionContext actionContext, ViewResult viewResult, string viewName, ViewEngineResult result)
 {
     if (result.Success)
     {
         DiagnosticListener.ViewFound(
             actionContext,
             isMainPage: true,
             viewResult,
             viewName,
             view: result.View);
     }
     else
     {
         DiagnosticListener.ViewNotFound(
             actionContext,
             isMainPage: true,
             viewResult,
             viewName,
             searchedLocations: result.SearchedLocations);
     }
 }
        /// <summary>
        /// Attempts to find the <see cref="IView"/> associated with <paramref name="viewResult"/>.
        /// </summary>
        /// <param name="actionContext">The <see cref="ActionContext"/> associated with the current request.</param>
        /// <param name="viewResult">The <see cref="PartialViewResult"/>.</param>
        /// <returns>A <see cref="ViewEngineResult"/>.</returns>
        public virtual ViewEngineResult FindView(ActionContext actionContext, PartialViewResult 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 ?? GetActionName(actionContext);

            var stopwatch = ValueStopwatch.StartNew();

            var result         = viewEngine.GetView(executingFilePath: null, viewPath: viewName, isMainPage: false);
            var originalResult = result;

            if (!result.Success)
            {
                result = viewEngine.FindView(actionContext, viewName, isMainPage: false);
            }

            Logger.PartialViewResultExecuting(result.ViewName);
            if (!result.Success)
            {
                if (originalResult.SearchedLocations.Any())
                {
                    if (result.SearchedLocations.Any())
                    {
                        // Return a new ViewEngineResult listing all searched locations.
                        var locations = new List <string>(originalResult.SearchedLocations);
                        locations.AddRange(result.SearchedLocations);
                        result = ViewEngineResult.NotFound(viewName, locations);
                    }
                    else
                    {
                        // GetView() searched locations but FindView() did not. Use first ViewEngineResult.
                        result = originalResult;
                    }
                }
            }

            if (result.Success)
            {
                DiagnosticListener.ViewFound(
                    actionContext,
                    isMainPage: false,
                    viewResult: viewResult,
                    viewName: viewName,
                    view: result.View);

                Logger.PartialViewFound(result.View, stopwatch.GetElapsedTime());
            }
            else
            {
                DiagnosticListener.ViewNotFound(
                    actionContext,
                    isMainPage: false,
                    viewResult: viewResult,
                    viewName: viewName,
                    searchedLocations: result.SearchedLocations);

                Logger.PartialViewNotFound(viewName, result.SearchedLocations);
            }

            return(result);
        }