An item with the same key has already been added error in the log

  • Updated

This article describes an issue where a production site displays the following errors in the log.

It appears there is a linkitemcollection of the type relatedlinks on some page which throws this error. Why is this error thrown and how is it resolved?

ERROR EPiServer.Global: 1.2.5 Unhandled exception in ASP.NET 
System.ArgumentException: An item with the same key has already been added. 
at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource) 
at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add) 
at EPiServer.Web.Mvc.Html.StructureHtmlHelperExtensions.HandleFrame(PageData page, RouteValueDictionary attributes) 
at EPiServer.Web.Mvc.Html.StructureHtmlHelperExtensions.SetRoutedDataAndHandleShortCutAndFrame(ContentReference contentLink, RouteValueDictionary values, RouteValueDictionary attributes, IContentLoader contentQueryable, LanguageSelectorFactory languageSelectorFactory) 
at EPiServer.Web.Mvc.Html.StructureHtmlHelperExtensions.ContentLink(HtmlHelper htmlHelper, String text, ContentReference contentLink, Object routeValues, Object htmlAttributes, IContentLoader contentLoader, LanguageSelectorFactory languageSelectorFactory, UrlResolver urlResolver) 
at EPiServer.Web.Mvc.Html.StructureHtmlHelperExtensions.PageLink(HtmlHelper htmlHelper, String text, PageReference pageLink, Object routeValues, Object htmlAttributes) 
at ASP._Page_Views_Shared_DisplayTemplates_LinkItemCollection_cshtml.Execute() in Views\Shared\DisplayTemplates\LinkItemCollection.cshtml:line 227 
at System.Web.WebPages.WebPageBase.ExecutePageHierarchy() 
at System.Web.Mvc.WebViewPage.ExecutePageHierarchy() 
at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) 
at System.Web.Mvc.Html.TemplateHelpers.ActionCacheViewItem.Execute(HtmlHelper html, ViewDataDictionary viewData) 
at System.Web.Mvc.Html.TemplateHelpers.ExecuteTemplate(HtmlHelper html, ViewDataDictionary viewData, String templateName, DataBoundControlMode mode, GetViewNamesDelegate getViewNames, GetDefaultActionsDelegate getDefaultActions) 
at System.Web.Mvc.Html.TemplateHelpers.TemplateHelper(HtmlHelper html, ModelMetadata metadata, String htmlFieldName, String templateName, DataBoundControlMode mode, Object additionalViewData, ExecuteTemplateDelegate executeTemplate) 
at System.Web.Mvc.Html.TemplateHelpers.TemplateHelper(HtmlHelper html, ModelMetadata metadata, String htmlFieldName, String templateName, DataBoundControlMode mode, Object additionalViewData) 
at System.Web.Mvc.Html.TemplateHelpers.TemplateFor[TContainer,TValue](HtmlHelper`1 html, Expression`1 expression, String templateName, String htmlFieldName, DataBoundControlMode mode, Object additionalViewData) 
at System.Web.Mvc.Html.DisplayExtensions.DisplayFor[TModel,TValue](HtmlHelper`1 html, Expression`1 expression) 
at EPiServer.Web.Mvc.Html.PropertyRenderer.<>c__DisplayClassa`2.<GetHtmlForDefaultMode>b__9(StringWriter writer) 
at EPiServer.Web.Mvc.Html.PropertyRenderer.CreateMvcHtmlString(Action`1 action) 
at EPiServer.Web.Mvc.Html.PropertyRenderer.PropertyFor[TModel,TValue](HtmlHelper`1 html, String viewModelPropertyName, Object additionalViewData, Object editorSettings, Expression`1 expression, Func`2 displayForAction) 
at EPiServer.Web.Mvc.Html.PropertyExtensions.PropertyFor[TModel,TValue](HtmlHelper`1 html, Expression`1 expression) 
at ASP._Page_Views_Shared_PagePartials_AsideContent_cshtml.Execute() in Views\Shared\PagePartials\AsideContent.cshtml:line 43 
at System.Web.WebPages.WebPageBase.ExecutePageHierarchy() 
at System.Web.Mvc.WebViewPage.ExecutePageHierarchy() 
at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) 
at System.Web.Mvc.Html.PartialExtensions.Partial(HtmlHelper htmlHelper, String partialViewName, Object model, ViewDataDictionary viewData) 
at ASP._Page_Views_Shared_Layout_cshtml.Execute() in Views\Shared\Layout.cshtml:line 259 
at System.Web.WebPages.WebPageBase.ExecutePageHierarchy() 
at System.Web.Mvc.WebViewPage.ExecutePageHierarchy() 
at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) 
at System.Web.WebPages.WebPageBase.<>c__DisplayClass7.<RenderPageCore>b__6(TextWriter writer) 
at System.Web.WebPages.WebPageBase.Write(HelperResult result) 
at System.Web.WebPages.WebPageBase.RenderSurrounding(String partialViewName, Action`1 body) 
at System.Web.WebPages.WebPageBase.PopContext() 
at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) 
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) 
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) 
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) 
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) 
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) 
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) 
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) 
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) 
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) 
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass1e.<BeginInvokeAction>b__1b(IAsyncResult asyncResult) 
at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) 
at System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) 
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) 
at System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) 
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) 
at System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) 
at System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__4(IAsyncResult asyncResult, ProcessRequestState innerState) 
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) 
at System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) 
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() 
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) 
System.ArgumentException: An item with the same key has already been added. 
at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource) 
at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add) 
at EPiServer.Web.Mvc.Html.StructureHtmlHelperExtensions.HandleFrame(PageData page, RouteValueDictionary attributes) 
at EPiServer.Web.Mvc.Html.StructureHtmlHelperExtensions.SetRoutedDataAndHandleShortCutAndFrame(ContentReference contentLink, RouteValueDictionary values, RouteValueDictionary attributes, IContentLoader contentQueryable, LanguageSelectorFactory languageSelectorFactory) 
at EPiServer.Web.Mvc.Html.StructureHtmlHelperExtensions.ContentLink(HtmlHelper htmlHelper, String text, ContentReference contentLink, Object routeValues, Object htmlAttributes, IContentLoader contentLoader, LanguageSelectorFactory languageSelectorFactory, UrlResolver urlResolver) 
at EPiServer.Web.Mvc.Html.StructureHtmlHelperExtensions.PageLink(HtmlHelper htmlHelper, String text, PageReference pageLink, Object routeValues, Object htmlAttributes) 
at ASP._Page_Views_Shared_DisplayTemplates_LinkItemCollection_cshtml.Execute() in Views\Shared\DisplayTemplates\LinkItemCollection.cshtml:line 227 
at System.Web.WebPages.WebPageBase.ExecutePageHierarchy() 
at System.Web.Mvc.WebViewPage.ExecutePageHierarchy() 
at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) 
at System.Web.Mvc.Html.TemplateHelpers.ActionCacheViewItem.Execute(HtmlHelper html, ViewDataDictionary viewData) 
at System.Web.Mvc.Html.TemplateHelpers.ExecuteTemplate(HtmlHelper html, ViewDataDictionary viewData, String templateName, DataBoundControlMode mode, GetViewNamesDelegate getViewNames, GetDefaultActionsDelegate getDefaultActions) 
at System.Web.Mvc.Html.TemplateHelpers.TemplateHelper(HtmlHelper html, ModelMetadata metadata, String htmlFieldName, String templateName, DataBoundControlMode mode, Object additionalViewData, ExecuteTemplateDelegate executeTemplate) 
at System.Web.Mvc.Html.TemplateHelpers.TemplateHelper(HtmlHelper html, ModelMetadata metadata, String htmlFieldName, String templateName, DataBoundControlMode mode, Object additionalViewData) 
at System.Web.Mvc.Html.TemplateHelpers.TemplateFor[TContainer,TValue](HtmlHelper`1 html, Expression`1 expression, String templateName, String htmlFieldName, DataBoundControlMode mode, Object additionalViewData) 
at System.Web.Mvc.Html.DisplayExtensions.DisplayFor[TModel,TValue](HtmlHelper`1 html, Expression`1 expression) 
at EPiServer.Web.Mvc.Html.PropertyRenderer.<>c__DisplayClassa`2.<GetHtmlForDefaultMode>b__9(StringWriter writer) 
at EPiServer.Web.Mvc.Html.PropertyRenderer.CreateMvcHtmlString(Action`1 action) 
at EPiServer.Web.Mvc.Html.PropertyRenderer.PropertyFor[TModel,TValue](HtmlHelper`1 html, String viewModelPropertyName, Object additionalViewData, Object editorSettings, Expression`1 expression, Func`2 displayForAction) 
at EPiServer.Web.Mvc.Html.PropertyExtensions.PropertyFor[TModel,TValue](HtmlHelper`1 html, Expression`1 expression) 
at ASP._Page_Views_Shared_PagePartials_AsideContent_cshtml.Execute() in Views\Shared\PagePartials\AsideContent.cshtml:line 43 
at System.Web.WebPages.WebPageBase.ExecutePageHierarchy() 
at System.Web.Mvc.WebViewPage.ExecutePageHierarchy() 
at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) 
at System.Web.Mvc.Html.PartialExtensions.Partial(HtmlHelper htmlHelper, String partialViewName, Object model, ViewDataDictionary viewData) 

According to the error log, there are 2 items that have the same key and it points to Views\Shared\DisplayTemplates\LinkItemCollection.cshtml:line 227 

and the line in the LinkItemCollection.cshtml is this:

 @Html.PageLink(linkItem.Text, basePage.PageLink, new { language = basePage.LanguageBranch }, new {@target = "_blank"})

The problem is that the "target" attribute will throw an exception later when trying to add target.
The solution is to check basePage.TargetFrameName to see if it has a value then don't add the target.