Rendering Controller

A basic implemenation of a controller is to set an Index() action that is resolving the current node and parse the node to the view to get data rendered.

ublic class PageController : Controller
{
    private IHttpContextAccessor _httpContextAccessor;
    private NodeAccess _nodeAccess;

    public PageController(IHttpContextAccessor httpContextAccessor, NodeAccess nodeAccess)
    {
        _httpContextAccessor = httpContextAccessor;
        _nodeAccess = nodeAccess;
    }

    public IActionResult Index()
    {
        var model = new PageViewModel();
     
        var currentPath = _httpContextAccessor.HttpContext.Request.Path.Value;

        model.RequestPath = currentPath;
        model.Node = GetNodeFromPath(currentPath);
        
        return View(model);
    }

    private NodeDataModel GetNodeFromPath(string path)
    {
        var rootItems = _nodeAccess.GetRootItems();
        var home = rootItems.FirstOrDefault(r => r.NodeType.Name.Equals("Home"));

        if (string.IsNullOrEmpty(path) || path.Equals("/"))
        {
            return home;
        }

        var current = home;

        foreach (var s in path.TrimStart('/').TrimEnd('/').Split('/'))
        {
            current = _nodeAccess.GetChildren(current.Id).FirstOrDefault(c => c.Name.Equals(s, StringComparison.OrdinalIgnoreCase));

            if (current == null)
            {
                break;
            }
        }

        return current;
    }
}