我正在使用 MVCSiteMapProvider 为我的应用程序生成具有本地化功能的菜单。只要菜单的资源文件位于 App_GlobalResources 文件夹中,我的程序就可以正常工作。当我将资源移至另一个文件夹时,出现错误提示无法找到资源。
我在用着$resources:Resource,menu_Home
用于访问 MVC.sitemap 文件中的资源。
我想将资源文件保留在自定义文件夹中,而不将它们存储在 App_GlobalResources 文件夹中。有人可以帮忙吗?
发生这种情况是因为 MvcSiteMapNode.Title 属性中的以下代码:
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;
}
在 GetExplicitResourceString() 方法中最后一个参数为 true,这意味着 throwIfNotFound。这就是抛出异常的原因。我通过将上面的代码替换为以下代码来修复它:
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;
}
}
在 .sitemap 文件内部,而不是 title="$resources:Resource,Key" 语法,使用 title="Namespace.Class.Property,Assembly" 语法,该语法更适合使用带有强类型生成类的嵌入式资源。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)