/// <summary> /// Initializes a new instance of the <see cref="XsltView"/> class. /// </summary> /// <param name="viewInfo">Object that provides context information required for this view.</param> public XsltView(ViewInfo viewInfo) { Contract.Requires<ArgumentNullException>(viewInfo != null); this.viewInfo = viewInfo; processor = viewInfo.Processor; }
/// <summary> /// Initializes the controller. /// </summary> /// <param name="requestContext">The request context.</param> protected override void Initialize(RequestContext requestContext) { base.Initialize(requestContext); string action = DefaultAction; string childPath = Regex.Replace(this.Context.Request.Path, "^" + this.Context.Request.ApplicationPath, string.Empty, RegexOptions.IgnoreCase); string testPath = string.Concat(this.Context.Path.ViewPath, childPath.TrimStart('/')); var pathParts = childPath.TrimStart('/').Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries); if (pathParts.Length >= 1) { this.name = pathParts[0]; } if (pathParts.Length >= 2) { action = string.Join("/", pathParts.Skip(1).ToArray()); if (Directory.Exists(this.Context.MapPath(testPath))) action = string.Join("/", action, DefaultAction); } this.genericViewInfo = this.GetViewInfo(action); if (!this.genericViewInfo.Exists && pathParts.Length == 1) { // try using home/<controller> path to see if that works action = this.name; this.name = DefaultController; var viewInfo2 = this.GetViewInfo(action); if (viewInfo2.Exists) { this.genericViewInfo = viewInfo2; } else { this.name = action; } } }
private ViewConfiguration(SageController controller, ViewInfo viewInfo) { Contract.Requires<ArgumentNullException>(controller != null); Contract.Requires<ArgumentNullException>(viewInfo != null); configElement = viewInfo.ConfigDocument.DocumentElement; this.Name = viewInfo.Action; this.Controller = controller; this.Info = viewInfo; this.Modules = new OrderedDictionary<string, IModule>(); if (string.IsNullOrWhiteSpace(ViewConfiguration.ModuleSelectXPath)) return; var moduleDependencies = new Dictionary<string, List<string>>(); var moduleNodes = this.Info.ConfigDocument.SelectNodes(ViewConfiguration.ModuleSelectXPath, XmlNamespaces.Manager); var moduleList = moduleNodes.ToList(); // tag all modules with unique ids for (int i = 0; i < moduleNodes.Count; i++) { XmlElement moduleElem = (XmlElement) moduleNodes[i]; string moduleId = moduleElem.GetAttribute("id"); if (string.IsNullOrEmpty(moduleId)) moduleId = string.Format(ModuleIdPattern, i); moduleElem.SetAttribute("id", moduleId); moduleDependencies.Add(moduleId, new List<string>()); } // determine module dependencies foreach (XmlElement moduleElem in moduleNodes) { string moduleId = moduleElem.GetAttribute("id"); var parent = moduleElem.ParentNode; while (parent != null) { if (moduleList.Contains(parent)) { string parentId = ((XmlElement) parent).GetAttribute("id"); moduleDependencies[parentId].Add(moduleId); moduleId = parentId; } parent = parent.ParentNode; } } // order the modules by processing order (nested modules processed first) moduleList = moduleList.OrderBy((node1, node2) => { var parentId = ((XmlElement) node1).GetAttribute("id"); var childId = ((XmlElement) node2).GetAttribute("id"); var contains = moduleDependencies[parentId].Contains(childId); return contains ? 1 : 0; }).ToList(); // create the modules and populate the modules dictionary SageModuleFactory factory = new SageModuleFactory(); foreach (XmlElement moduleElem in moduleList) { var moduleId = moduleElem.GetAttribute("id"); IModule module = factory.CreateModule(moduleElem); this.Modules.Add(moduleId, module); log.DebugFormat("Mapped element {0} to module {1} ({2}).", moduleElem.LocalName, module.GetType().Name, module.GetType().FullName); } }
/// <summary> /// Creates a new <see cref="ViewConfiguration"/> instance for the specified <paramref name="controller"/> and /// <paramref name="viewInfo"/>. /// </summary> /// <param name="controller">The controller for which to create the view configuration.</param> /// <param name="viewInfo">The object that contains the information about the view.</param> /// <returns> /// A new <see cref="ViewConfiguration"/> instance for the specified <paramref name="controller"/> and /// <paramref name="viewInfo"/>. /// </returns> public static ViewConfiguration Create(SageController controller, ViewInfo viewInfo) { return new ViewConfiguration(controller, viewInfo); }
/// <summary> /// Processes the view descibed with the specified <paramref name="viewInfo"/>, /// and returns an <see cref="ActionResult"/>. /// </summary> /// <param name="viewInfo">The object that contains information about the view.</param> /// <returns>The action result.</returns> public ActionResult SageView(ViewInfo viewInfo) { log.DebugFormat("Running view '{0}' on controller '{1}'.", viewInfo.Action, this.Name); if (!viewInfo.Exists) { log.FatalFormat("The specified view name '{0}' doesn't exist.", viewInfo); return this.PageNotFound(); } var cache = this.Context.ViewCache; var caching = this.Context.ProjectConfiguration.ViewCaching; var checkCache = !viewInfo.IsNoCacheView && !this.Context.IsNoCacheRequest && caching.Enabled; var localName = this.Context.LocalPath; if (checkCache) { var cacheDate = cache.GetLastModified(localName, ViewCache.HtmlGroup); var isCached = cacheDate != null && cacheDate.Value > viewInfo.LastModified; if (isCached) { var time = DateTime.Now; var cachedContent = cache.Get(localName, ViewCache.HtmlGroup); log.DebugFormat("View {0} loaded from cache in {1}ms", localName, (DateTime.Now - time).Milliseconds); return new ContentResult { Content = cachedContent, ContentType = "text/html", ContentEncoding = Encoding.UTF8 }; } } ViewModel model = this.GetViewModel(viewInfo); return this.View(model.Action, model); }
/// <summary> /// Processes the view configuration associated with the specified <paramref name="viewName"/>, /// and returns an <see cref="ActionResult"/>. /// </summary> /// <param name="viewName">The name of the view that should be rendered to response.</param> /// <returns>The action result.</returns> public ActionResult SageView(string viewName) { ViewInfo viewInfo = new ViewInfo(this, viewName); return this.SageView(viewInfo); }
/// <summary> /// Sets the HTTP status to not found (404) and returns an <see cref="EmptyResult" />. /// </summary> /// <param name="action">Optional name of the action that could not be found.</param> /// <returns>Empty result</returns> public ActionResult PageNotFound(string action = null) { this.Context.Response.StatusCode = 404; this.Context.Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache); this.Context.Response.Cache.SetNoStore(); var view404 = new ViewInfo(this, "404", "_system"); if (view404.Exists) { var info = new ViewInfo(this, "404", "_system"); viewInfos.Add("404", info); ViewModel result = this.GetViewModel(info); return this.View(info.Action, result); } if (this.Context.ProjectConfiguration.ErrorViews.ContainsKey("404")) { var view = this.Context.ProjectConfiguration.ErrorViews["404"]; var content = this.Context.IsDeveloperRequest ? view.DeveloperContent : view.DefaultContent; content = this.Context.ProcessText(content); var errorMessage = content.Substitute(new NameValueCollection { { "path", this.Context.Request.Path }, { "controller", this.Name }, { "action", action }, }); return new ContentResult { Content = errorMessage, ContentType = "text/html" }; } return new EmptyResult(); }
/// <summary> /// Processes the view configuration associated with the specified <paramref name="viewInfo"/>, /// and returns a <see cref="ViewModel"/> instance that contains the result. /// </summary> /// <param name="viewInfo">The object that contains information about the view.</param> /// <returns> /// An object that contains the result of processing the view configuration /// </returns> public virtual ViewModel GetViewModel(ViewInfo viewInfo) { try { var startTime = DateTime.Now.Ticks; ViewConfiguration config = ViewConfiguration.Create(this, viewInfo); var result = config.Process(); var elapsed = new TimeSpan(DateTime.Now.Ticks - startTime); log.DebugFormat("Completed processing view {0} in {1}ms", viewInfo.Action, elapsed.Milliseconds); return result; } catch (Exception ex) { throw new SageHelpException(new ProblemInfo(ProblemType.ViewProcessingError), ex); } }