Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • stevo 63 posts 106 karma points
    Jul 13, 2015 @ 01:00
    stevo
    0

    customize routing in section tree controller

    Hi,

    I developing my first backend extension for Umbraco. I'm struggle already with TreeController. I have three level deep tree structure. But I can't make the routing work. Umbraco looks in the views folder...

    Error message: Request error: The URL returned a 404 (not found): views/raceseries/edit.html

    Tree navigation:

    • Race Series 1 (/mayhem/raceseries/edit/Race+Series+1)
      • 2015 (/mayhem/raceyear/edit/2015)
        • Race 1 (/mayhem/race/edit/1)

    Folder structure:

    • App_Plugins
      • Mayhem
        • BackOffice
          • RaceSeries
            • edit.html
          • RaceYear
            • edit.html
          • Race
            • edit.html

    TreeController:

    [PluginController("Mayhem")]
    [Tree("mayhem", "Mayhem", "Races", "icon-message", "icon-message", true, 5)]
    public class MayhemTreeController : TreeController
    {
        protected override TreeNodeCollection GetTreeNodes(string id, FormDataCollection queryStrings)
        {
            var nodes = new TreeNodeCollection();
    
            if (id == Constants.System.Root.ToString())
            {
                var node1 = CreateTreeNode(WebUtility.UrlEncode("Race Series 1"), null, queryStrings, "Race Series 1", "icon-tags", true);
                node1.RoutePath = "/mayhem/raceseries/edit/" + WebUtility.UrlEncode("Race Series 1");
                nodes.Add(node1);
    
                var node2 = CreateTreeNode(WebUtility.UrlEncode("Race Series 2"), null, queryStrings, "Race Series 2", "icon-tags", true);
                node2.RoutePath = "/mayhem/raceseries/edit/" + WebUtility.UrlEncode("Race Series 2");
                nodes.Add(node2);
            }
            else
            {
                // get years from race series
                if (!id.IsNumbic())
                {
                    int[] years = {2014, 2015};
    
                    foreach (var item in years)
                    {
                        var node = CreateTreeNode(item.ToString(), id, queryStrings, item.ToString(), "icon-message", true);
                        node.RoutePath = "/mayhem/raceyear/edit/" + item;
                        nodes.Add(node);
                    }
                }
                else // get all races
                {
                    var raceNode1 = CreateTreeNode("123", id, queryStrings, "06-28 Race 2", "icon-message", false);
                    raceNode1.RoutePath = "/mayhem/race/edit/123";
                    nodes.Add(raceNode1);
    
                    var raceNode2 = CreateTreeNode("124", id, queryStrings, "05-06 Race 1", "icon-message", false);
                    raceNode2.RoutePath = "/mayhem/race/edit/124";
                    nodes.Add(raceNode2);
                }
            }
    
            return nodes;
        }
    

    Our help would be greatly appreciated, Thanks Stefan

  • David Brendel 792 posts 2970 karma points MVP 3x c-trib
    Jul 13, 2015 @ 06:37
    David Brendel
    0

    Hi stevo,

    i think the routing to the views also include the Name of youre tree. So you have to add a folder named "mayhem" to the backoffice folder and move your currently folder to the "mayhem" folder.

    Regards David

  • stevo 63 posts 106 karma points
    Jul 13, 2015 @ 07:58
    stevo
    0

    Hi Dave!

    Thanks for your answer! I tried it. It didn't work. But I found the answer here.

    /:section/:tree/:method/:id /App_Plugins/{mypackage}/BackOffice/{treetype}/{method}.html

    I had to changed my folder structure.

    TreeController

    [PluginController("Mayhem")]
    [Tree("mayhem", "race", "Races", "icon-message", "icon-message", true, 5)]
    public class RaceTreeController : TreeController
    {
        private readonly IRaceService _raceService;
    
        public RaceTreeController(IRaceService raceService)
        {
            _raceService = raceService;
        }
    
        protected override TreeNodeCollection GetTreeNodes(string id, FormDataCollection queryStrings)
        {
            var nodes = new TreeNodeCollection();
    
            //check if we're rendering the root node's children
            if (id == Constants.System.Root.ToString())
            {
                foreach (var series in _raceService.GetAllRaceSeries())
                {
                    var hasChildNodes = series.Value.Count > 0;
                    var nodeToAdd = CreateTreeNode(WebUtility.UrlEncode(series.Key), null, queryStrings, series.Key, "icon-tags", hasChildNodes);
                    nodeToAdd.RoutePath = "/mayhem/race/series/" + WebUtility.UrlEncode(series.Key);
                    nodes.Add(nodeToAdd);
                }
            }
            else
            {
                // get years from race series
                if (!id.IsNumbic())
                {
                    var series = _raceService.GetAllRaceSeries(id).Reverse();
                    foreach (var item in series)
                    {
                        var treeNode = CreateTreeNode(item.Year.ToString(), id, queryStrings, item.Id.ToString(), "icon-message", true);
                        treeNode.RoutePath = "/mayhem/race/year/" + item.Year;
                        nodes.Add(treeNode);
                    }
                }
                else // get all races
                {
                    var treeNode = CreateTreeNode("123", id, queryStrings, "05-06 B-Line", "icon-message", false);
                    nodes.Add(treeNode);
                }
            }
    
            return nodes;
        }
    
        protected override MenuItemCollection GetMenuForNode(string id, FormDataCollection queryStrings)
        {
            var menu = new MenuItemCollection();
    
            //If the node is the root node (top of tree)
            if (id == Constants.System.Root.ToInvariantString())
            {
                //Add in refresh action
                menu.Items.Add<RefreshNode, ActionRefresh>(ui.Text("actions", ActionRefresh.Instance.Alias), false);
            }
    
            return menu;
        }
    }
    }
    

    Important is the second parameter by the Tree declaration:

    That is the tree name in the url (case sensitive!! )

    [Tree("mayhem", "race", "Races", "icon-message", "icon-message", true, 5)]

    nodeToAdd.RoutePath = "/mayhem/race/series/" + WebUtility.UrlEncode(series.Key);

    Folder structure:

    • App_Plugins
      • Mayhem
        • BackOffice
          • Race
            • edit.html
            • year.html
            • series.html

    It's not perfect but I works. Cheers, Stefan

  • Taylor Mitchell 19 posts 43 karma points
    Oct 12, 2015 @ 19:05
    Taylor Mitchell
    0

    Any idea how to add an additional querystring to the url? I tried to do it manually and the "?" ended up url encoded

  • David Brendel 792 posts 2970 karma points MVP 3x c-trib
    Oct 12, 2015 @ 19:33
    David Brendel
    0

    Hi Taylor.

    Think you have to add them as "/param" to work.

    Regards David

Please Sign in or register to post replies

Write your reply to:

Draft