private static PageIdentity IdentifyPage(IDisplayHelper displayHelper) { return(displayHelper.HttpContext.GetItem("PageIdentity", () => { var context = displayHelper.HttpContext; var routeValues = context.GetRouteData().Values; var controllerName = routeValues.GetControllerName().ToLowerInvariant(); var actionName = routeValues.GetActionName().ToLowerInvariant(); string currentPageType = "system"; object currentPageId = controllerName + "." + actionName; if (displayHelper.IsHomePage()) { currentPageType = "home"; currentPageId = 0; } else if (controllerName == "catalog") { if (actionName == "category") { currentPageType = "category"; currentPageId = routeValues.Get("categoryId"); } else if (actionName == "manufacturer") { currentPageType = "brand"; currentPageId = routeValues.Get("manufacturerId"); } } else if (controllerName == "product") { if (actionName == "productdetails") { currentPageType = "product"; currentPageId = routeValues.Get("productId"); } } else if (controllerName == "topic") { if (actionName == "topicdetails") { currentPageType = "topic"; currentPageId = routeValues.Get("topicId"); } } return new PageIdentity { CurrentPageId = currentPageId, CurrentPageType = currentPageType }; })); }
/// <summary> /// Find the selected node in any registered menu /// </summary> public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) { var executedContext = await next(); if (context.HttpContext.Request.IsAjaxRequest()) { return; } //if (filterContext.HttpContext.Request.HttpMethod != "GET") // return; if (!executedContext.Result.IsHtmlViewResult()) { return; } if (executedContext.RouteData.Values.GetAreaName().EqualsNoCase("admin")) { return; } var selectedNode = await ResolveCurrentNodeAsync(executedContext); object nodeData; if (selectedNode == null) { nodeData = new { type = _displayHelper.CurrentPageType(), id = _displayHelper.CurrentPageId() }; } else { var httpContext = context.HttpContext; // So that other actions/partials can access this. httpContext.Items["SelectedNode"] = selectedNode; // Add custom meta head part (mainly for client scripts) var nodeType = (selectedNode.Value.EntityName ?? _displayHelper.CurrentPageType()).ToLowerInvariant(); object nodeId = selectedNode.Id; if (_displayHelper.IsHomePage()) { nodeId = 0; } else if (nodeType == "system") { nodeId = _displayHelper.CurrentPageId(); } nodeData = new { type = nodeType, id = nodeId, menuItemId = selectedNode.Value.MenuItemId, entityId = selectedNode.Value.EntityId, parentId = selectedNode.Parent?.IsRoot == true ? 0 : selectedNode.Parent?.Id }; } // Add node data to head meta property as JSON. _assetBuilder.AddHtmlContent("head", new HtmlString("<meta property='sm:pagedata' content='{0}' />".FormatInvariant(JsonConvert.SerializeObject(nodeData)))); }