0

I have the following class

public class MenuVeiculo
{
    public string Nome { get; set; }
    public string NomeEn { get; set; }
    public Guid ID { get; set; }
}

As you can see, I have two properties, "Nome" and "NomeEn." Each one represents the name and the name in English.

Mvc.sitemap

<mvcSiteMapNode key="MenuVeiculo" dynamicNodeProvider="Semep.Extensibilidade.SiteMap.MenuVeiculoDynamicNodeProvider, Semep" title="Menu veiculo" action="Index" controller="Rental">

MenuVeiculoDynamicNodeProvider.cs

public class MenuVeiculoDynamicNodeProvider : DynamicNodeProviderBase
{
    public override IEnumerable<DynamicNode> GetDynamicNodeCollection()
    {
        var context = DependencyResolver.Current.GetService<SemepContext>();
        var listDB = (from p in context.MenusVeiculo
                        select new
                                    {
                                        p.Nome,
                                        p.ID
                                    });

        const string keyFormat = "MenuVeiculo_{0}";
        foreach (var menu in listDB.ToList())
        {
            var key = string.Format(keyFormat, menu.ID.ToString().ToUpper());
            var root = new DynamicNode(key, menu.Nome)
                            {
                                Title = menu.Nome
                                ,
                                Key = key
                            };
            root.Attributes.Add("id", menu.ID.ToString());
            root.RouteValues.Add("id", menu.ID);
            yield return root;
        }
    }

    #endregion
}

Question

My question is, how to work with multi-language and DynamicNodeProviderBase? As you can see, there are two fields, and I'm only showing one. A problem of "Thread.CurrentThread.CurrentCulture" is that the MvcSiteMapProvider caches the result, how to handle this:

ridermansb
  • 10,779
  • 24
  • 115
  • 226

2 Answers2

0

Yes this is one of the shortcomings of MvcSiteMapProvider. In a project I've solved this by returning all the nodes, one for each localization, and using a custom VisibilityProvider to only show the correct localization.

Xharze
  • 2,703
  • 2
  • 17
  • 30
0

You need to create an additional Route with lang parameter:

    routes.MapRoute(
        name: "Default_lang",
        url: "{lang}/{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
        constraints: new { lang = @"^(en|ru)$" },
        namespaces: new[] { "PNSoft.WebSite.Controllers" }
    );

Then, in your mvc.sitemap you specify lang="..." parameter for the root node and for child nodes you need to set inheritedRouteParameters="lang" and then you can get lang from node RouteValues property:

        public override IEnumerable<MvcSiteMapProvider.DynamicNode> GetDynamicNodeCollection(MvcSiteMapProvider.ISiteMapNode node)
        {
            var lang = (string)node.RouteValues["lang"];
...
        }

Thats all!

Pavel Nazarov
  • 723
  • 6
  • 10