2010-04-04 36 views
2

创建资源经理,我想我的网页上创建资源管理器,并使用存储在我的资源文件中的一些数据。 (为Default.aspx.resx和default.aspx.en.resx)如何在ASP.NET

的代码看起来是这样的:

System.Resources.ResourceManager myResourceManager = System.Resources.ResourceManager.CreateFileBasedResourceManager("resource", 
       Server.MapPath("App_LocalResources") + Path.DirectorySeparatorChar, null); 

    if (User.Identity.IsAuthenticated) 
    { 
     Welcome.Text = myResourceManager.GetString("LoggedInWelcomeText"); 
    } 
    else 
    { 
     Welcome.Text = myResourceManager.GetString("LoggedOutWelcomeText"); 
    } 

,但是当我编译和我的本地服务器上运行它,我得到这个类型的错误:

Could not find any resources appropriate for the specified culture (or the neutral culture) on disk. baseName: resource locationInfo: fileName: resource.resources Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

异常详细信息:System.Resources.MissingManifestResourceException:找不到任何资源适合指定区域性(或中性文化)在磁盘上。 baseName的:资源locationInfo:文件名:resource.resources

源错误:

89行:别的90 行:{ 线91:Welcome.Text = myResourceManager.GetString( “LoggedOutWelcomeText”); 92行:} 93行:

请问您可以帮我解决这个问题吗?

回答

1

MSDN,CreateFileBasedResourceManager期待已编译的资源文件。通常,将一个.resx文件放在App_LocalResources中。在这个目录中是否有名为resource.XXX.resources(其中XXX是当前文化ID)的目录中的文件?

我没有VS才能在ASP.NET项目来检查这个方便的,但处理资源字符串通常的方式是要走的项目属性,资源选项卡,并在其中创建的字符串。这将在您的资源周围生成强类型的包装,避免需要显式创建自己的ResourceManager。

+0

是的,我有default.aspx.en.resx和App_LocalResources文件为Default.aspx.resx同时还有名称为“LoggedOutWelcomeText” – Bart 2010-04-04 14:28:37

+0

OK行,但名称不要”从“资源”开始,这就是你对CreateFileBasedResourceManager的调用所告诉的内容。 – Timores 2010-04-05 09:35:01

3

使用GetLocalResourceObject()它是System.Web.Page的基础的一部分。 如果你想进入全球资源利用GetGlobalResourceObject()。

保持它的简单;-)