Root folder name problem

Discussion in 'ASP.NET 2.0' started by Tonyg, Feb 17, 2010.

  1. Hello.

    I have a website on discountAsp.Net developed using VS2008 Pro, SP1, .NET 2.0, ASP.NET 2.0, WSE 3.0.

    When i test in my development environment and on web sites other than discountAsp.Net, i have code(Taken from Dan Clem's web site administration tool) to display my web folders as such:

    <asp:TreeView runat="server" ID="FolderTree"
    OnSelectedNodeChanged="FolderTree_SelectedNodeChanged" >
    <RootNodeStyle ImageUrl="~/Images/folder.gif" />
    <ParentNodeStyle ImageUrl="~/Images/folder.gif" />
    <LeafNodeStyle ImageUrl="~/Images/folder.gif" />
    <SelectedNodeStyle Font-Underline="true" ForeColor="#A21818" />
    </asp:TreeView>

    It displays the root as "CoyneWebServices" which is the name of my top-level folder for my web site in VS2008 and it displays all of the other folders under that one OK.

    After i upload my web site to discountAsp.Net, the same function displays the root as "htdocs" as the top-level folder.

    Does anybody know how to make it display the same on any hosting machine?

    Any help would be gratefully appreciated.

    Thanks,
    Tony
     
  2. 'htdocs' is the name of the root folder in your account on the DASP web server. I haven't seen the code you're using but it sounds to me like it's working as it should. When you ran it and 'CoyneWebServices' was the root folder name means that 'CoyneWebServices' was the name of the web root folder relative to where the web application was running from at that time.

    If you want to display some common meaningful root folder name (e.g. Root) regardless of the platform where the code is running, you'll have to make a change to the code - probably in the Treeview.TreeNodeDataBound event, if your code is databinding.
     
  3. Hello Joe.

    I see what you are saying. I can't find a TreeNodeDatabound event.

    I want to research this further to see if i can just hardwire it or get the web site name somehow. My problem is that all of the code for that function is written in C# and the rest of my code is in VB. I'm not good at VB and i'm worst with C#.

    Thanks for your help.

    Tony
     
  4. Post a link to where you got the code from. If I can work out what it's doing, I'll post the solution back here.
     
  5. Hi Tony,

    I visited the link you posted and downloaded the sample code on that page from http://aspnet.4guysfromrolla.com/code/ezdeploy.zip

    I got the project running locally and believe you were describing the admin/access/access_rules.aspx page that includes the Treeview that renders the website folder structure. This is the page that allows role / user based access rules to be maintained for the folders in the website.

    I note that when I first downloaded the project it was zipped into a folder called 'Simple' so that when I first ran the project, I saw the Treeview root TreeNode was called 'Simple' with all of the sub folders inside it. A quick test I performed was to rename the 'Simple' folder to 'WAT' (Website Admin Tool) and execute the project again to verify that the root tree node was really using the folder name in the file system. This was the case, however I also found at this point that all of the hyper links in the web site no longer worked because the original developer had hardcoded the folder name 'Simple' throughout the code and markup. No problem; a quick find and replace in Visual Studio sorted this.

    Ok onto the original question. How do you ensure that a common root folder name is used (e.g. Root) regardless of the server or folder structure this code runs on? These are the steps I took:

    • I added a root web config appSetting to define a common name that would be used for the root folder in the TreeView:
      Code:
      <appSettings>
          <add key="AccessRulesAdminRootFolderName" value="Root"/>
      </appSettings>
      
    • I modified the AddNodeAndDescendents method in the access_rules.aspx page very slightly to use this new appSetting for the TreeView root node. This is my modified version of the AddNodeAndDescendents method:
      Code:
      private TreeNode AddNodeAndDescendents(DirectoryInfo folder, TreeNode parentNode)
      	{
      		/*
      		 * Dan Clem, 4/15/2007.
      		 * The PopulateTree and AddNodeAndDescendents are taken almost verbatim from Scott Mitchell's article
      		 * "Using the TreeView Control and a DataList to Create an Online Image Gallery", which is located at 
      		 * http://aspnet.4guysfromrolla.com/articles/083006-1.aspx
      		 */
      		
      		 // Add the TreeNode, displaying the folder's name and storing the full path to the folder as the value...
      		
      		string virtualFolderPath;
              //Are we rendering the root node?
              bool isRootNode = false;
      		if (parentNode == null)
      		{
      			virtualFolderPath = VirtualImageRoot;
                  //this is the root node
                  isRootNode = true;
      		}
      		else
      		{
      			virtualFolderPath = parentNode.Value + folder.Name + "/";
      		}
      
              //if we are rendering the root node, use the value from the AccessRulesAdminRootFolderName web.config for the root node text
              //otherwise just use the folder name as is from the file system
              string folderName = isRootNode ? ConfigurationManager.AppSettings["AccessRulesAdminRootFolderName"] : folder.Name;
      
              TreeNode node = new TreeNode(folderName, virtualFolderPath);
      		node.Selected = (folder.Name == selectedFolderName);
      		
      		// Recurse through this folder's subfolders
      		DirectoryInfo[] subFolders = folder.GetDirectories();
      		foreach(DirectoryInfo subFolder in subFolders)
      		{
      			if (subFolder.Name != "_controls" && subFolder.Name != "App_Data")
      			{
      				TreeNode child = AddNodeAndDescendents(subFolder, node);
      				node.ChildNodes.Add(child);
      			}
      		}
      		return node; // Return the new TreeNode
      	}
      
    Luckily the original code was written to use the TreeNode.Value for the folders rather than the TreeNode.Text which means it was safe to use any text value for any of the TreeNodes without impacting on the code that performs role management on the folders.

    Locally and uploaded to a new website on the DASP server - in both cases it seemed to be working fine.

    HTH.
     
  6. Joe,

    Thanks alot for this. I made the changes and it works excellent.

    Thanks for all of your effort on this. I very much appreciate the time you put into it.

    Tony
     

Share This Page