/// <summary> /// Renders the View with either an ITreeNode model or the given Model Type /// </summary> /// <returns></returns> public ActionResult RenderViewWithModel(bool?IncludeDocumentInOutputCache = null) { if (!IncludeDocumentInOutputCache.HasValue) { IncludeDocumentInOutputCache = DynamicRouteInternalHelper.GetDefaultAddPageToCacheDependency(); } var node = mDynamicRouteHelper.GetPage(AddPageToCacheDependency: IncludeDocumentInOutputCache.Value); var routeConfig = mDynamicRouteHelper.GetRouteConfiguration(node); HttpContext.Kentico().PageBuilder().Initialize(node.DocumentID); // Convert type if (routeConfig.ModelType != null) { try { return(View(routeConfig.ViewName, Convert.ChangeType(node, routeConfig.ModelType))); } catch (InvalidCastException ex) { throw new InvalidCastException(ex.Message + ", this may be caused by the generated PageType class not being found in the project, or if it's located in an assembly that does not have [assembly: AssemblyDiscoverable] in it's AssemblyInfo.cs. The found page is of type " + (node == null ? "Null" : node.GetType().FullName), ex); } } else { return(View(routeConfig.ViewName, node)); } }
/// <summary> /// Renders the Dynamic Route View (no model) /// </summary> /// <returns></returns> public ActionResult RenderView(bool?IncludeDocumentInOutputCache = null) { if (!IncludeDocumentInOutputCache.HasValue) { IncludeDocumentInOutputCache = DynamicRouteInternalHelper.GetDefaultAddPageToCacheDependency(); } // Get default Add Page to Output Dependency var node = mDynamicRouteHelper.GetPage(AddPageToCacheDependency: IncludeDocumentInOutputCache.Value); var routeConfig = mDynamicRouteHelper.GetRouteConfiguration(node); HttpContext.Kentico().PageBuilder().Initialize(node.DocumentID); return(View(routeConfig.ViewName)); }
/// <summary> /// Gets the node based on the current request url and then renders the template result. /// </summary> public ActionResult Index(string TemplateControllerName = null, bool?IncludeDocumentInOutputCache = null) { if (!IncludeDocumentInOutputCache.HasValue) { IncludeDocumentInOutputCache = DynamicRouteInternalHelper.GetDefaultAddPageToCacheDependency(); } ITreeNode FoundNode = mDynamicRouteHelper.GetPage(Columns: new string[] { "DocumentID" }, AddPageToCacheDependency: IncludeDocumentInOutputCache.Value); if (FoundNode != null) { HttpContext.Kentico().PageBuilder().Initialize(FoundNode.DocumentID); if (!string.IsNullOrWhiteSpace(TemplateControllerName)) { // Adjust the route data to point to the template's controller if it has one. HttpContext.Request.RequestContext.RouteData.Values["Controller"] = TemplateControllerName; } return(new TemplateResult(FoundNode.DocumentID)); } else { return(new HttpNotFoundResult()); } }
/// <summary> /// Determines where the Controller and Action should be based on the dynamic routing data /// </summary> /// <param name="node"></param> /// <returns></returns> private DynamicRouteConfiguration ResolveRouteValues(ITreeNode node) { string defaultController = RequestContext.RouteData.Values.ContainsKey("controller") ? RequestContext.RouteData.Values["controller"].ToString() : ""; string defaultAction = RequestContext.RouteData.Values.ContainsKey("action") ? RequestContext.RouteData.Values["action"].ToString() : ""; if (string.IsNullOrWhiteSpace(defaultController)) { defaultController = "DynamicRoute"; } if (string.IsNullOrWhiteSpace(defaultAction)) { defaultAction = "RouteValuesNotFound"; } if (node is null) { return(new DynamicRouteConfiguration(defaultController, defaultAction, null, null, DynamicRouteType.Controller, false, false)); } if (PageHasTemplate(node)) { var PageTemplateRouteConfig = new DynamicRouteConfiguration("DynamicRouteTemplate", "Index", null, null, DynamicRouteType.Controller, DynamicRouteInternalHelper.GetDefaultAddPageToCacheDependency(), false); string PageTemplateControllerName = GetPageTemplateController(node); // When the Dynamic Route Template Controller renders the Page Template, the Route Controller needs to match or it won't look in the right spot for the view if (!string.IsNullOrWhiteSpace(PageTemplateControllerName)) { PageTemplateRouteConfig.RouteValues.Add("TemplateControllerName", PageTemplateControllerName); } return(PageTemplateRouteConfig); } if (!DynamicRoutingAnalyzer.TryFindMatch(node.ClassName, out var match)) { return(new DynamicRouteConfiguration(defaultController, defaultAction, null, null, DynamicRouteType.Controller, false, false)); } return(match); }
/// <summary> /// Returns an error message when the page is found but no DynamicRouting assembly tags were configured. /// </summary> /// <returns></returns> public ActionResult RouteValuesNotFound() { var node = mDynamicRouteHelper.GetPage(AddPageToCacheDependency: DynamicRouteInternalHelper.GetDefaultAddPageToCacheDependency()); return(Content($"<h1>No Route Value Found</h1><p>No DynamicRouting assembly tag was found for the class <strong>{node.ClassName}</strong>, could not route page {node.NodeAliasPath}</p>")); }