问题
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?
回答1:
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.
来源:https://stackoverflow.com/questions/11292155/resource-file-for-mvcsitemapprovider