Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

Routes values are being added in duplicate #3250

Closed
mdmoura opened this issue Oct 3, 2015 · 3 comments
Closed

Routes values are being added in duplicate #3250

mdmoura opened this issue Oct 3, 2015 · 3 comments
Assignees
Milestone

Comments

@mdmoura
Copy link

mdmoura commented Oct 3, 2015

Hello,

I created a TagHelper and needed to access the current route so I ended up with:

    [TargetElement("a", Attributes = RouteHasName)]
    public class RouteActiveTagHelper : TagHelper {

      private const String RouteHasName = "route-has";

      private IActionContextAccessor _actionContextAccessor;
      private IUrlHelper _urlHelper;   

      [HtmlAttributeName(RouteHasName)]
      public String RouteHas { get; set; }

      public RouteActiveTagHelper(IActionContextAccessor actionContextAccessor, IUrlHelper urlHelper) {
        _actionContextAccessor = actionContextAccessor;
        _urlHelper = urlHelper;
    } // RouteActiveTagHelper

    public override void Process(TagHelperContext context, TagHelperOutput output) {

      var values = _actionContextAccessor.ActionContext.RouteData.Values;

      String route = _urlHelper.RouteUrl(values.Distinct());  

      // Removed remaining code for sake of simplicity

      } // Process
    } // RouteActiveTagHelper

When I create the route I get the error "ArgumentException: An item with the same key has already been added." on every route which is defined using Attribute Routing.

To fix this I needed to apply Distinct to values before creating a route.

Is there any reason for having duplicated objects in route values? Is this a bug?

If this is the expected result is there a better way to get the current route values?

Note: I get the same error using ViewContext ...

Thank You,
Miguel

@rynowak
Copy link
Member

rynowak commented Oct 3, 2015

This seems like a bug. Can you include a callstack for the exception?

As a workaround, if you're just trying to link to the 'current values' you don't need to pass them in to RouteUrl.

_urlHelper.RouteUrl(new { })

@mdmoura
Copy link
Author

mdmoura commented Oct 3, 2015

Here is the stack. Does it help?

    ArgumentException: An item with the same key has already been added.
        System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
            System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
            System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value)
            Microsoft.AspNet.Mvc.Routing.InnerAttributeRoute.GenerateVirtualPath(VirtualPathContext context, AttributeRouteLinkGenerationEntry entry)
            Microsoft.AspNet.Mvc.Routing.InnerAttributeRoute.GetVirtualPath(VirtualPathContext context)
            Microsoft.AspNet.Mvc.Routing.AttributeRoute.GetVirtualPath(VirtualPathContext context)
            Microsoft.AspNet.Routing.RouteCollection.GetVirtualPath(VirtualPathContext context)
            Microsoft.AspNet.Mvc.UrlHelper.GeneratePathFromRoute(String routeName, IDictionary`2 values)
            Microsoft.AspNet.Mvc.UrlHelper.RouteUrl(UrlRouteContext routeContext)
            Microsoft.AspNet.Mvc.UrlHelperExtensions.RouteUrl(IUrlHelper helper, String routeName, Object values, String protocol, String host, String fragment)
            Microsoft.AspNet.Mvc.UrlHelperExtensions.RouteUrl(IUrlHelper helper, Object values)
            MvcProject.Web.ActiveRouteTagHelper.Process(TagHelperContext context, TagHelperOutput output) in ActiveRouteTagHelper.cs
      public override void Process(TagHelperContext context, TagHelperOutput output) {
        IDictionary<String, Object> values = _actionContextAccessor.ActionContext.RouteData.Values;
        String route = _urlHelper.RouteUrl(values).ToLowerInvariant();
        Boolean match = false;
        if (!String.IsNullOrWhiteSpace(RouteIs)) {
            match = route == RouteIs;
            Microsoft.AspNet.Razor.Runtime.TagHelpers.TagHelper.< ProcessAsync > d__4.MoveNext()
            -- End of stack trace from previous location where exception was thrown ---
            System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
                System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
                System.Runtime.CompilerServices.TaskAwaiter.GetResult()
                Microsoft.AspNet.Razor.Runtime.TagHelpers.TagHelperRunner.< RunAsync > d__0.MoveNext()
    -- End of stack trace from previous location where exception was thrown ---
    System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
        System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
        System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
        Asp.ASPV__Views_Shared__Prime_Main_cshtml.<> c__DisplayClass28_0.<< ExecuteAsync > b__0 > d.MoveNext() in _Prime.Main.cshtml
                                            }
    ---End of stack trace from previous location where exception was thrown ---
    System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
        System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
        System.Runtime.CompilerServices.TaskAwaiter.GetResult()
        Microsoft.AspNet.Mvc.Razor.RazorPage.< RenderSectionAsyncCore > d__93.MoveNext()
        -- - End of stack trace from previous location where exception was thrown ---
            System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
        System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
        System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
        Microsoft.AspNet.Mvc.Razor.RazorPage.RenderSection(String name, Boolean required)
        Asp.ASPV__Views_Shared__Prime_cshtml.<< ExecuteAsync > b__21_0 > d.MoveNext() in _Prime.cshtml
            @RenderSection("Body", required: true)
            ---End of stack trace from previous location where exception was thrown ---
            System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
            System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
            System.Runtime.CompilerServices.TaskAwaiter.GetResult()
            Microsoft.AspNet.Mvc.Razor.RazorPage.< RenderSectionAsyncCore > d__93.MoveNext()
            -- - End of stack trace from previous location where exception was thrown ---
                System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
            System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
            System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
            Microsoft.AspNet.Mvc.Razor.RazorPage.RenderSection(String name, Boolean required)
            Asp.ASPV__Views_Shared__Empty_cshtml.< ExecuteAsync > d__29.MoveNext() in _Empty.cshtml
                @RenderSection("Body", required: true)
            ---End of stack trace from previous location where exception was thrown ---
            System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
            System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
            System.Runtime.CompilerServices.TaskAwaiter.GetResult()
            Microsoft.AspNet.Mvc.Razor.RazorView.< RenderPageCoreAsync > d__17.MoveNext()
            -- - End of stack trace from previous location where exception was thrown ---
                System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
            System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
            System.Runtime.CompilerServices.TaskAwaiter.GetResult()
            Microsoft.AspNet.Mvc.Razor.RazorView.< RenderPageAsync > d__16.MoveNext()
            -- - End of stack trace from previous location where exception was thrown ---
                System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
            System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
            System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
            Microsoft.AspNet.Mvc.Razor.RazorView.< RenderLayoutAsync > d__19.MoveNext()
            -- - End of stack trace from previous location where exception was thrown ---
                System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
            System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
            System.Runtime.CompilerServices.TaskAwaiter.GetResult()
            Microsoft.AspNet.Mvc.Razor.RazorView.< RenderAsync > d__15.MoveNext()
            -- - End of stack trace from previous location where exception was thrown ---
                System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
            System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
            System.Runtime.CompilerServices.TaskAwaiter.GetResult()
            Microsoft.AspNet.Mvc.ViewExecutor.< ExecuteAsync > d__1.MoveNext()
            -- - End of stack trace from previous location where exception was thrown ---
                System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
            System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
            System.Runtime.CompilerServices.TaskAwaiter.GetResult()
            Microsoft.AspNet.Mvc.ViewResult.< ExecuteResultAsync > d__24.MoveNext()
            -- - End of stack trace from previous location where exception was thrown ---
                System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
            System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
            System.Runtime.CompilerServices.TaskAwaiter.GetResult()
            Microsoft.AspNet.Mvc.Core.FilterActionInvoker.< InvokeResultAsync > d__53.MoveNext()
            -- - End of stack trace from previous location where exception was thrown ---
                System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
            System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
            System.Runtime.CompilerServices.TaskAwaiter.GetResult()
            Microsoft.AspNet.Mvc.Core.FilterActionInvoker.< InvokeResultFilterAsync > d__52.MoveNext()
            -- - End of stack trace from previous location where exception was thrown ---
                System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
            Microsoft.AspNet.Mvc.Core.FilterActionInvoker.< InvokeAllResultFiltersAsync > d__51.MoveNext()
            -- - End of stack trace from previous location where exception was thrown ---
                System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
            System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
            System.Runtime.CompilerServices.TaskAwaiter.GetResult()
            Microsoft.AspNet.Mvc.Core.FilterActionInvoker.< InvokeResourceFilterAsync > d__46.MoveNext()
            -- - End of stack trace from previous location where exception was thrown ---
                System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
            Microsoft.AspNet.Mvc.Core.FilterActionInvoker.< InvokeAsync > d__41.MoveNext()
            -- - End of stack trace from previous location where exception was thrown ---
                System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
            System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
            System.Runtime.CompilerServices.TaskAwaiter.GetResult()
            Microsoft.AspNet.Mvc.MvcRouteHandler.< InvokeActionAsync > d__7.MoveNext()
            -- - End of stack trace from previous location where exception was thrown ---
                System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
            System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
            System.Runtime.CompilerServices.TaskAwaiter.GetResult()
            Microsoft.AspNet.Mvc.MvcRouteHandler.< RouteAsync > d__6.MoveNext()
            -- - End of stack trace from previous location where exception was thrown ---
                System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
            System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
            System.Runtime.CompilerServices.TaskAwaiter.GetResult()
            Microsoft.AspNet.Mvc.Routing.InnerAttributeRoute.< RouteAsync > d__10.MoveNext()
            -- - End of stack trace from previous location where exception was thrown ---
                System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
            System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
            System.Runtime.CompilerServices.TaskAwaiter.GetResult()
            Microsoft.AspNet.Routing.RouteCollection.< RouteAsync > d__9.MoveNext()
            -- - End of stack trace from previous location where exception was thrown ---
                System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
            System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
            System.Runtime.CompilerServices.TaskAwaiter.GetResult()
            Microsoft.AspNet.Builder.RouterMiddleware.< Invoke > d__4.MoveNext()
            -- - End of stack trace from previous location where exception was thrown ---
                System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
            System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
            System.Runtime.CompilerServices.TaskAwaiter.GetResult()
            Microsoft.AspNet.Diagnostics.ErrorPageMiddleware.< Invoke > d__6.MoveNext()

@rynowak
Copy link
Member

rynowak commented Oct 4, 2015

Thanks, that will help us narrow it down.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants