Esempio n. 1
0
            /// <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))));
            }