Solved

Web.sitemap - hiding default.aspx but still expanding tree view and breadcrumbs navigation

Posted on 2008-10-18
7
1,009 Views
Last Modified: 2012-05-05
I have a sitemap, a tree view and a bread crumb control.  In my sitemap I have URLs with the default document suffixed, that is

<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0">
  <siteMapNode url="~/default.aspx" title="Home" description="">

    <siteMapNode title="One" url="~/One/default.aspx">
      <siteMapNode title="Two" url="~/Two/default.aspx">
        ...
etc



The tree view allows me to navigate to these URLs correctly.  However, the tree view is too stupid to understand that / really means /default.asppx, and therefore if I remove the "default.aspx" the treeview does not expand correctly.

What I want is to have the links in my tree view not contain the default.aspx suffix, yet to have the treeview and the breadcrumb expand correctly and show the user where they currently are.


If I change my sitemap to remove the default documents, ie to be this:
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0">
  <siteMapNode url="~/" title="Home" description="">

    <siteMapNode title="One" url="~/One/">
      <siteMapNode title="Two" url="~/Two/">
        ...

then the tree view does not expand the node correctly and the breadcrumb shows nothing.  The treeview expands itsself correctly accordingt o my code, but it refused to recognise that it's at the correct node.  Therefore the current node is not bold and, moreover, the breadscrumb always shows nothing.

Here is my code:



<asp:SiteMapDataSource ID="SiteMapDataSource3" runat="server"  />
       
....
 <asp:TreeView OnTreeNodeDataBound="treeMainMenu_TreeNodeDataBound" ID="TV"
                             SelectedNodeStyle-Font-Bold=true  ExpandImageToolTip="{0}"
                            runat="server" DataSourceID="SiteMapDataSource3"
                            PopulateNodesFromClient="False" ExpandDepth="1" EnableViewState="False"
                                LineImagesFolder="~/i/L/" ShowLines="True"  SkipLinkText="">
                            <ParentNodeStyle ChildNodesPadding="0px"  
                            HorizontalPadding="1px"  />
                            <LeafNodeStyle  VerticalPadding="0px"   HorizontalPadding="1px"    />
                           
                        </asp:TreeView>

....




  protected void  treeMainMenu_TreeNodeDataBound
           ( object  sender ,  System.Web.UI.WebControls.TreeNodeEventArgs e)
        {

            if (
                    (Request.Path.ToLower() == e.Node.NavigateUrl.ToLower())
                     ||
                     (
                        e.Node.NavigateUrl.ToLower().EndsWith("/")
                        &&
                        Request.Path.ToLower() == e.Node.NavigateUrl.ToLower() + "default.aspx"                        
                     )
                )  
            {
                e.Node.ExpandAll();
                e.Node.Parent.ExpandAll();
                // These two lines will prevent self-referencing links to the current URL
                e.Node.SelectAction = TreeNodeSelectAction.None;
                e.Node.NavigateUrl = null;                
            }
        }


Please advise.
0
Comment
Question by:dc197
  • 4
  • 3
7 Comments
 
LVL 3

Expert Comment

by:JunkMan
ID: 22762704
You need to make your sitemap provider folder aware:
//add to your app_code folder

public class FolderAware_XmlSiteMapProvider : XmlSiteMapProvider

{

    public override SiteMapNode CurrentNode

    {

        get

        {

            SiteMapNode currentNode = base.CurrentNode;

            if (currentNode == null)

            {

                HttpContext context = HttpContext.Current;

                if (context != null)

                {

                    string text = context.Request.RawUrl.ToLowerInvariant();

                    int index = text.IndexOf("/default.aspx");

                    if (index != -1)

                    {

                        currentNode = FindSiteMapNode(text.Substring(0, index));

                        if (currentNode != null && !currentNode.IsAccessibleToUser(context))

                        {

                            currentNode = null;

                        }

                    }

                }

            }

            return currentNode;

        }

    }

}
 

//on your web.config:

		<siteMap defaultProvider="FolderAware_XmlSiteMapProvider" enabled="true">

			<providers>

				<clear/>

				<add siteMapFile="Web.sitemap" name="FolderAware_XmlSiteMapProvider" type="FolderAware_XmlSiteMapProvider"/>

			</providers>

		</siteMap>

Open in new window

0
 
LVL 5

Author Comment

by:dc197
ID: 22762939
I have already tried that and it doesn't work.  The call to FindSiteMapNode always returns null.


0
 
LVL 3

Accepted Solution

by:
JunkMan earned 150 total points
ID: 22763015
make sure the paths don't finish in /.

if that doesn't work, send us your site map..
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 5

Author Comment

by:dc197
ID: 22763669
The paths in my sitemap do indeed finish with /.  That's perfectly legitimate.

In fact if ones makes a request for site.com/folder then IIS will redirect the browser to site.com/folder/ and only then will this sencond request, for /folder/, result in /folder/default/aspx being executed.


Nevertheless, tonight at home I shall try the new sitemap provided without trailing slashes in my sitemap, and will revert.

Cheers


0
 
LVL 5

Author Comment

by:dc197
ID: 22775061
Really great stuff.  Works almost perfectly.  The last little niggle - how to I ensure that the node for the home page works?  In my site map it used to have this:

 <siteMapNode url="~/default.aspx" title="MySite.com" description="Home page">


I want this node, too, to work as all the others.  I have tried url="~/" and url="http://mySite.com" and url="~" but the bread crumb and tree view dislike all of these.  Is there a secret I'm missing?


Many thanks

0
 
LVL 3

Expert Comment

by:JunkMan
ID: 22776372
umm the only thing i can think of is the setup of your treeview... with the very simple version attached, i managed to get Home on the breadcrumb and also the treeview..
//the sitemap:

<?xml version="1.0" encoding="utf-8" ?>

<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >

    <siteMapNode url="~/default.aspx" title="Home"  description="">

      <siteMapNode url="~/one" title="One"  description="" >

        <siteMapNode url="~/one/child" title="One child"  description="" />

      </siteMapNode>

    </siteMapNode>

</siteMap>
 

//the pages:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title></title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

        <asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" />

        <asp:SiteMapPath ID="SiteMapPath1" runat="server" >

        </asp:SiteMapPath>

        <div style="float: left; width: 250px">

            <asp:TreeView ID="TreeView1" runat="server" DataSourceID="SiteMapDataSource1">

            </asp:TreeView>

        </div>

        <div style="float: left;">

            <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">

            </asp:ContentPlaceHolder>

        </div>

    </div>

    </form>

</body>

</html>

Open in new window

0
 
LVL 5

Author Comment

by:dc197
ID: 22829511
Not to worry.  I'll just use the fully qualified absolute URL for the home node.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Error in page 3 46
Remove greater than sign 3 50
Why is some text in blue in Visual Studio? 6 25
Displaying a web form as a modal popup dialog box. 4 25
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now