Creating child nodes for a DynamicNode in MvcSiteMapProvider that have dynamic parameters

落爺英雄遲暮 提交于 2019-12-02 15:39:00

I am using version 1.x. I had a similar problem with dynamic parameters.

I had to modify the source code - made a change in MvcSiteMapNode.cs. This is the new implementation of Url property

    public override string Url
    {
        get
        {
            if (!string.IsNullOrEmpty(this.url))
                return this.url;

            RequestContext ctx;
            if (HttpContext.Current.Handler is MvcHandler)
                ctx = ((MvcHandler)HttpContext.Current.Handler).RequestContext;
            else
                ctx = new RequestContext(new HttpContextWrapper(HttpContext.Current), new RouteData());

            var routeValues = new RouteValueDictionary(RouteValues);

            foreach (var key in DynamicParameters)
                routeValues.Add(key, ctx.RouteData.Values[key]);

            return new UrlHelper(ctx).Action(Action, Controller, routeValues);
        }
        set
        {
            this.url = value;
        }
    }

Notice how actual values of dynamicParameters are added to routeValues collection.

The above change allowed me to define dynamic paremteres in sitemap (like 'id') and create breadcrumbs with links like Account/User/Edit/23.

I took a brief look at version 2.x and you should be able to apply similar patch. Hope it will help you...

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!