2

I'm using MVCSiteMapProvider for generating menus with localization for my application. My program works fine as long as the resource files for the menus are in the App_GlobalResources folder. When I move the resources into another folder, it gives an error mentioning not able to find the resources. I'm using $resources:Resource,menu_Home for accessing the resources in the MVC.sitemap file. I want to keep the resource files in a custom folder without storing them in the App_GlobalResources folder. Can anyone help?

SDK
  • 175
  • 1
  • 15
  • I don't think this can be done, as this is done using the default localization feature built-in to the XmlSiteMapProvider in the .NET framework, see http://msdn.microsoft.com/en-us/library/ms178427(v=vs.100).aspx. But if that can be changed it would work. – Xharze Jul 02 '12 at 11:01
  • I've Searched everywhere... Seems like that. :-/ – SDK Jul 02 '12 at 11:09
  • I have the same problem. I cannot understand why it has not been workarounded, since resources in MVC applications are better placed elsewhere, but in App_GlobalResources, for testing purposes. – Jaime Nov 20 '13 at 15:26

1 Answers1

0

it happens because of the following code in MvcSiteMapNode.Title property:

var implicitResourceString = GetImplicitResourceString("title");
if (implicitResourceString != null && implicitResourceString == this["title"])
{
    return implicitResourceString;
}
implicitResourceString = GetExplicitResourceString("title", this["title"], true);
if (implicitResourceString != null && implicitResourceString == this["title"])
{
    return implicitResourceString;
}

In GetExplicitResourceString() method last parameter is true, which means throwIfNotFound. That's why exception is thrown. I fixed it by replacing the code above with the following code:

if (!string.IsNullOrEmpty(title))
{
    if (Provider.EnableLocalization)
    {
        try
        {
            if (!string.IsNullOrEmpty(title) && title.Contains("."))
            {
                int idx = title.LastIndexOf(",");
                string res = title.Substring(0, idx);
                string assembly = title.Substring(idx + 1);

                idx = res.LastIndexOf(".");
                string type = res.Substring(0, idx);
                string key = res.Substring(idx + 1);
                var rm = new ResourceManager(type, Assembly.Load(assembly));
                return rm.GetString(key);
            }
        }
        catch
        {
        }
        return title;
    }
}

And inside .sitemap file instead of title="$resources:Resource,Key" syntax use title="Namespace.Class.Property,Assembly" syntax which is more suitable when use use embedded resources with strong-type generated class.

alex
  • 187
  • 1
  • 9