对于MVCSiteMapProvider资源文件 [英] Resource file for MVCSiteMapProvider

查看:191
本文介绍了对于MVCSiteMapProvider资源文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用MVCSiteMapProvider与本地化我的应用程序生成菜单。我的程序,只要在菜单中的资源文件中的文件夹App_GlobalResources文件工作正常。当我移动的资源投入到其他文件夹,它给出了一个错误提没能找到资源。
我使用 $资源:资源,menu_Home 在MVC.sitemap文件访问的资源。
我想保持在一个自定义文件夹中的资源文件,而无需将它们存储在App_GlobalResources文件夹中。任何人都可以帮忙吗?

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?

推荐答案

这是因为在MvcSiteMapNode.Title财产以下code的:

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;
}

在GetExplicitResourceString()方法最后一个参数是true,这意味着throwIfNotFound。这就是为什么异常。

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;
    }
}

和.sitemap文件,而不是标题=$资源:资源,重点里面的语法使用标题=Namespace.Class.Property,集结号语法时,使用采用嵌入式资源具有很强的类型生成的类哪个更适合。

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.

这篇关于对于MVCSiteMapProvider资源文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆