Resource file for MVCSiteMapProvider

℡╲_俬逩灬. 提交于 2019-12-06 05:26:57

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.

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