Problem working with MvcSiteMap

Mar 26, 2010 at 6:04 PM
Edited Mar 26, 2010 at 6:12 PM

I was able to work it perfectly if I have my sitemap file like this:

<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="http://mvcsitemap.codeplex.com/schemas/MvcSiteMap-File-1.0" enableLocalization="true">
  <mvcSiteMapNode title="$resources:SiteMapLocalizations,HomeTitle" controller="Home" action="Index" isDynamic="true" dynamicParameters="*" >
</mvcSiteMapNode>
        <mvcSiteMapNode title="$resources:SiteMapLocalizations,ProductsTitle" controller="Products" >
      <mvcSiteMapNode title="$resources:SiteMapLocalizations,ProductsListTitle"
                      controller="Products" action="List"
                      isDynamic="true" dynamicParameters="id"
                      visibility="InSiteMapPathOnly"
                      key="ProductsListCategory"/>
    </mvcSiteMapNode>

    <mvcSiteMapNode title="$resources:SiteMapLocalizations,AccountTitle" controller="Account" visibility="InSiteMapPathOnly" imageUrl="~/Content/Icons/user.png">
      <!-- No need to specify controller here if not needed: parent controller name will be used -->
      <mvcSiteMapNode title="$resources:SiteMapLocalizations,LoginTitle" action="LogOn" />
      <mvcSiteMapNode title="$resources:SiteMapLocalizations,AccountCreationTitle" action="Register" />
      <mvcSiteMapNode title="$resources:SiteMapLocalizations,ChangePasswordTitle" action="ChangePassword" />
      <mvcSiteMapNode title="$resources:SiteMapLocalizations,LogoutTitle" action="LogOff" />
    </mvcSiteMapNode>

    <mvcSiteMapNode title="$resources:SiteMapLocalizations,SitemapTitle" controller="Home" action="Sitemap" visibility="InSiteMapPathOnly" />

    <mvcSiteMapNode title="$resources:SiteMapLocalizations,AboutUsTitle" controller="Home" action="About" />
    
  </mvcSiteMapNode>
</siteMap>

 

If I do like below( I just changed the mvcSiteMapNode end tag of Home to not include any other menu items under it):

 

<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="http://mvcsitemap.codeplex.com/schemas/MvcSiteMap-File-1.0" enableLocalization="true">
  <mvcSiteMapNode title="$resources:SiteMapLocalizations,HomeTitle" controller="Home" action="Index" isDynamic="true" dynamicParameters="*" >
</mvcSiteMapNode>
        <mvcSiteMapNode title="$resources:SiteMapLocalizations,ProductsTitle" controller="Products" >
      <mvcSiteMapNode title="$resources:SiteMapLocalizations,ProductsListTitle"
                      controller="Products" action="List"
                      isDynamic="true" dynamicParameters="id"
                      visibility="InSiteMapPathOnly"
                      key="ProductsListCategory"/>
    </mvcSiteMapNode>

    <mvcSiteMapNode title="$resources:SiteMapLocalizations,AccountTitle" controller="Account" visibility="InSiteMapPathOnly" imageUrl="~/Content/Icons/user.png">
      <!-- No need to specify controller here if not needed: parent controller name will be used -->
      <mvcSiteMapNode title="$resources:SiteMapLocalizations,LoginTitle" action="LogOn" />
      <mvcSiteMapNode title="$resources:SiteMapLocalizations,AccountCreationTitle" action="Register" />
      <mvcSiteMapNode title="$resources:SiteMapLocalizations,ChangePasswordTitle" action="ChangePassword" />
      <mvcSiteMapNode title="$resources:SiteMapLocalizations,LogoutTitle" action="LogOff" />
    </mvcSiteMapNode>

    <mvcSiteMapNode title="$resources:SiteMapLocalizations,SitemapTitle" controller="Home" action="Sitemap" visibility="InSiteMapPathOnly" />

    <mvcSiteMapNode title="$resources:SiteMapLocalizations,AboutUsTitle" controller="Home" action="About" />
    
 
</siteMap>

 

I get the following warning at the line marked as Red and my menu just displays Home tab and doesn't display other menus such as About us and Products.

The element 'siteMap' in namespace 'http://mvcsitemap.codeplex.com/schemas/MvcSiteMap-File-1.0' has invalid child element 'mvcSiteMapNode' in namespace 'http://mvcsitemap.codeplex.com/schemas/MvcSiteMap-File-1.0'.

Mar 27, 2010 at 3:04 AM

You are missing your closing brace on the xml element.

You have:

<mvcSiteMapNode  title="$resources:SiteMapLocalizations,ProductsTitle" controller="Products" >

Should be:

<mvcSiteMapNode  title="$resources:SiteMapLocalizations,ProductsTitle" controller="Products" />

or

<mvcSiteMapNode  title="$resources:SiteMapLocalizations,ProductsTitle" controller="Products" ><mvcSiteMapNode/>

Mar 27, 2010 at 6:24 PM

Well, I already have a end tag as you can see(marked in blue):

<mvcSiteMapNode title="$resources:SiteMapLocalizations,ProductsTitle" controller="Products" >
      <mvcSiteMapNode title="$resources:SiteMapLocalizations,ProductsListTitle"
                      controller="Products" action="List"
                      isDynamic="true" dynamicParameters="id"
                      visibility="InSiteMapPathOnly"
                      key="ProductsListCategory"/>
    </mvcSiteMapNode>

 

Is it needed that I need to include all mvcsitemap nodes under one root node? as I want home menu should be just separate without including any other menu items under it.  The reason is, I am using a java script function to generate the drop down menus and it works as such, that, if it see's any <li> under <ul> then it's gonna generate menu drop down under it, and I don't want that to happen with home menu displaying all the menus under it. Is there a workaround?

Mar 27, 2010 at 7:18 PM

Sorry, I see your end tag... :-)

I have a situation sort of like yours.  I solve this in two different ways (for 2 different needs).

1)  the Html.Menu's 3rd parameter is 'maxLevels' - set that to 1 and it will not render anything but the top node(s).

2)  I have two sitemap files.  I put two entries in web.config, then render the appropriate items by using each provider.  Works good.

 

Will those work?

Mar 27, 2010 at 7:50 PM

The first solution won't work for me because I need to have submenu's generated for other menu items but not for Home. And for the second one, you mean I put just the home menu in one sitemap file and others in other sitemap file and render those two? But the problem is these menu's will be separate and if I render them, I have to adjust the styles to look  them all coming from the same source. Let me know if any other way you can suggest me? Thanks.

Mar 27, 2010 at 8:25 PM

Put everything in a 'dummy' sitemapnode like:

 

<mvcSiteMapNode url="~/default.aspx" title="None" description="" roles="*" visibility="InSiteMapPathOnly">

    <mvcSiteMapNode HOME.... />

    <mvcSiteMapNode other menu... >

         <mvcSiteMapNode submenu of other menu.... />

    <mvcSiteMapNode/>

</mvcSiteMapNode>

The visibility will make it not render outer most one!

Mar 29, 2010 at 3:09 PM

Thank you, it worked for me.