An item with the same key has already been added

Jan 26, 2011 at 4:38 PM

Using Changeset 56685, I get a "An item with the same key has already been added" error with this simple sitemap file. Any idea why?

<?xml version="1.0" encoding="utf-8" ?>
<mvcSiteMap xmlns="http://mvcsitemap.codeplex.com/schemas/MvcSiteMap-File-3.0" enableLocalization="false">
  <mvcSiteMapNode title="Home" controller="Home" action="Index" changeFrequency="Always" updatePriority="Normal">
    <mvcSiteMapNode title="Why Buy Your New Home" Controller="WhyBuyYourNewHome" Action="Index" />
  </mvcSiteMapNode>
</mvcSiteMap>

[ArgumentException: An item with the same key has already been added.]
System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add) +12673712
System.Collections.Generic.Dictionary`2..ctor(IDictionary`2 dictionary, IEqualityComparer`1 comparer) +267
System.Web.Routing.RouteValueDictionary..ctor(IDictionary`2 dictionary) +62
MvcSiteMapProvider.DefaultSiteMapNodeUrlResolver.ResolveUrl(MvcSiteMapNode mvcSiteMapNode, String area, String controller, String action, IDictionary`2 routeValues) in C:\sitemap\Branches\3.0.0\MvcSiteMapProvider\MvcSiteMapProvider\DefaultSiteMapNodeUrlResolver.cs:77
MvcSiteMapProvider.MvcSiteMapNode.get_Url() in C:\sitemap\Branches\3.0.0\MvcSiteMapProvider\MvcSiteMapProvider\MvcSiteMapNode.cs:271
MvcSiteMapProvider.DefaultSiteMapProvider.AddNode(SiteMapNode node, SiteMapNode parentNode) in C:\sitemap\Branches\3.0.0\MvcSiteMapProvider\MvcSiteMapProvider\DefaultSiteMapProvider.cs:391
MvcSiteMapProvider.DefaultSiteMapProvider.ProcessXmlNodes(SiteMapNode rootNode, XElement rootElement) in C:\sitemap\Branches\3.0.0\MvcSiteMapProvider\MvcSiteMapProvider\DefaultSiteMapProvider.cs:645
MvcSiteMapProvider.DefaultSiteMapProvider.BuildSiteMap() in C:\sitemap\Branches\3.0.0\MvcSiteMapProvider\MvcSiteMapProvider\DefaultSiteMapProvider.cs:488

[MvcSiteMapException: An error occured while building the sitemap... Check the InnerException for more details.]
MvcSiteMapProvider.DefaultSiteMapProvider.BuildSiteMap() in C:\sitemap\Branches\3.0.0\MvcSiteMapProvider\MvcSiteMapProvider\DefaultSiteMapProvider.cs:574
System.Web.StaticSiteMapProvider.FindSiteMapNode(String rawUrl) +271
MvcSiteMapProvider.DefaultSiteMapProvider.FindSiteMapNode(HttpContext context, RouteData routeData) in C:\sitemap\Branches\3.0.0\MvcSiteMapProvider\MvcSiteMapProvider\DefaultSiteMapProvider.cs:1013
MvcSiteMapProvider.DefaultSiteMapProvider.FindSiteMapNode(HttpContext context) in C:\sitemap\Branches\3.0.0\MvcSiteMapProvider\MvcSiteMapProvider\DefaultSiteMapProvider.cs:980
System.Web.SiteMapProvider.get_CurrentNode() +69
MvcSiteMapProvider.Web.Html.SiteMapPathHelper.SiteMapPath(MvcSiteMapHtmlHelper helper, String templateName) in C:\sitemap\Branches\3.0.0\MvcSiteMapProvider\MvcSiteMapProvider\Web\Html\SiteMapPathHelper.cs:44
MvcSiteMapProvider.Web.Html.SiteMapPathHelper.SiteMapPath(MvcSiteMapHtmlHelper helper) in C:\sitemap\Branches\3.0.0\MvcSiteMapProvider\MvcSiteMapProvider\Web\Html\SiteMapPathHelper.cs:33
ASP._Page_Views_Shared_SiteMaster_cshtml.Execute() in d:\Main\Source\UI.Web\Views\Shared\SiteMaster.cshtml:110
System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +280
System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +104
System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +173
System.Web.WebPages.WebPageBase.RenderSurrounding(String partialViewName, Action`1 body) +209
System.Web.WebPages.WebPageBase.PopContext() +222
System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +384
System.Web.Mvc.<>c__DisplayClass1c.<InvokeActionResultWithFilters>b__19() +33
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +784900
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +784900
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +784900
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +265
System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +784976
System.Web.Mvc.Controller.ExecuteCore() +159
System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +335
System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +62
System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +20
System.Web.Mvc.<>c__DisplayClasse.<EndProcessRequest>b__d() +54
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +453
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +371

Jan 26, 2011 at 10:21 PM

So this apparently has to do with my "catchall" route. If I remove it, the error goes away but then I have no home page and the site map complains about not being able to map the home page node to a working route. :) Here's the route definition:

            routes.MapNamedRoute(
                "CatchAll", // Route name
                "{*catchall}", // URL with parameters
                new { controller = "Home", action = "Index" } // Parameter defaults
            );

Coordinator
Jan 27, 2011 at 9:04 AM

Controller and action should be a unique combination, so with catch-all this will not work.

You can edit your sitemap file like this,  I *think*  this will work. Just add a value for the catchall parameter.

<?xml version="1.0" encoding="utf-8" ?>
<mvcSiteMap xmlns="http://mvcsitemap.codeplex.com/schemas/MvcSiteMap-File-3.0" enableLocalization="false">
  <mvcSiteMapNode title="Home" controller="Home" action="Index" changeFrequency="Always" updatePriority="Normal">
    <mvcSiteMapNode title="Why Buy Your New Home" Controller="WhyBuyYourNewHome" Action="Index" catchall="why-buy-your-new-home" />
  </mvcSiteMapNode>
</mvcSiteMap>
Jan 27, 2011 at 4:21 PM

That didn't fix it, unfortunately. I'll keep investigating, but if you have any other ideas, I'm all ears. :)

Jan 27, 2011 at 8:34 PM

So with the following XML:

<?xml version="1.0" encoding="utf-8" ?>
<mvcSiteMap xmlns="http://mvcsitemap.codeplex.com/schemas/MvcSiteMap-File-3.0" enableLocalization="true">
    <mvcSiteMapNode title="Why Buy Your New Home" Controller="WhyBuyYourNewHome" Action="Index">
      <mvcSiteMapNode title="Why Our Homes" Controller="WhyBuyYourNewHome" Action="WhyOurHomes" />
      <mvcSiteMapNode title="New Vs. Pre-Owned" Controller="WhyBuyYourNewHome" Action="NewVsPreOwned" />
      <mvcSiteMapNode title="First Time Buyer" Controller="WhyBuyYourNewHome" Action="FirstTimeBuyer" />
      <mvcSiteMapNode title="Move Up Buyer" Controller="WhyBuyYourNewHome" Action="MoveUpBuyer" />
      <mvcSiteMapNode title="Relocation Buyer" Controller="WhyBuyYourNewHome" Action="RelocationBuyer" />
    </mvcSiteMapNode>
</mvcSiteMap>

I get the following error:

Could not resolve URL for sitemap node Why Buy Your New Home which represents action  in controller . Ensure that the route  for this sitemap node can be resolved and that its default values allow resolving the URL for the current sitemap node.

Note how the values for action and controller that should appear in the error message are blank. Is it possible that somehow the controller and action values are not coming through correctly, which would explain why it's falling to the catchall route in the first place?

Jan 27, 2011 at 8:43 PM

So it was because "Controller" and "Action" should have been all lowercase. Grrrr....thanks for the help anyways. :)