/// <summary> /// Attempts to find the document (view) using all currently registered view engines. /// </summary> /// <param name="documentName">Name of the view to be found</param> /// <param name="result">The view result object.</param> /// <param name="locationsSearchedUnsuccessfully">A list of locations the engines searched unsuccessfully (can be used by callers to display error messages and the like)</param> /// <returns>Success indicator (false if no view was found)</returns> private bool FindDocument(string documentName, DocumentResult result, List<string> locationsSearchedUnsuccessfully = null) { var foundDocument = false; var controllerName = GetType().Name.ToLower(); if (controllerName.ToLower().EndsWith("controller")) controllerName = controllerName.Substring(0, controllerName.Length - 10); foreach (var engine in RegisteredViewEngines) { var documentEngine = engine as IDocumentEngine; if (documentEngine == null) continue; var documentResult = documentEngine.GetDocument(documentName, controllerName); if (documentResult.FoundDocument) { result.Document = documentResult.Document; result.ViewSource = documentResult.DocumentSource; result.Document.DataContext = result.Model; foundDocument = true; break; } if (locationsSearchedUnsuccessfully != null) locationsSearchedUnsuccessfully.AddRange(documentResult.LocationsSearched); } return foundDocument; }
/// <summary>Returns a named document (view) associated with the current action and assigns the model</summary> /// <param name="documentName">Name of the document.</param> /// <param name="model">The model.</param> /// <returns>Document Result.</returns> protected virtual DocumentResult Document(string documentName, object model = null) { var result = new DocumentResult {Model = model}; var locationsSearchedUnsuccessfully = new List<string>(); if (!FindDocument(documentName, result, locationsSearchedUnsuccessfully)) { var sb = new StringBuilder(); foreach (var location in locationsSearchedUnsuccessfully) sb.AppendLine(location); throw new DocumentNotFoundException(sb.ToString()); } return result; }