开发者

ASP.NET HttpContext.GetLocalResourceObject() throws InvalidOperationException

Let's say we have such site structure:

App_LocalResources

      |- A.aspx.resx

      |- B.aspx.resx

A.aspx

B.aspx

Now I use HttpContext.GetLocalResourceObject("~/A.aspx", "Key1") in A.aspx.cs, and it works fine. But if I use HttpContext.GetLocalResourceObject("~/A.aspx", "Key1") in B.aspx.cs, it throws an exception:

The resource class for this page was not found. Please check if the resource file exists and try again.

Exception Details: System.InvalidOperationException: The resource class for this page was not found. Please check if the resource file exists and try again.

How can I resolve this problem? I want to read the local resources from an external page, and I don't want to read the .resx file myself. Thanks :-)

UPDATE: In my case, there're some "data.xml" files(they are in different directories, and have elements like <key name='Key1' value='value1' />), and the contents of them will be rendered as html.

But the key names in the data.xml should be localized before rendering (different data.xml contain different keys).

For example, the data.xml has such an ele开发者_运维问答ment:

<key name='CategoryId' value='3' />

In the result html page, I want to display "Category Id = 3" for en-US culture, and "类别=3" for zh-CN culture, etc.

So I think I can create some files following the pattern "data.xml.??-??.resx" in the App_LocalResources folder, then use the HttpContext.GetLocalResource() for each data.xml to retrieve the localized key names. That way I don't need to read the xml myself. Is it possible?


That's not the way that local resources are supposed to be used. Local resources are only valid for a page or control. You should use global resources in your case.

From MSDN

Global Resource Files

You create a global resource file by putting it in the reserved folder App_GlobalResources at the root of the application. Any .resx file that is in the App_GlobalResources folder has global scope. Additionally, ASP.NET generates a strongly typed object that gives you a simple way to programmatically access global resources.

Local Resource Files

A local resources file is one that applies to only one ASP.NET page or user control (an ASP.NET file that has a file-name extension of .aspx, .ascx, or .master). You put local resource files in folders that have the reserved name App_LocalResources. Unlike the root App_GlobalResources folder, App_LocalResources folders can be in any folder in the application. You associate a set of resources files with a specific Web page by using the name of the resource file.

And could be also useful for you to check how access resources programatically

    Button1.Text = 
        GetLocalResourceObject("Button1.Text").ToString();
    Image1.ImageUrl = 
        (String)GetGlobalResourceObject(
        "WebResourcesGlobal", "LogoUrl");
    Image1.Visible = true;


As Claudio Redi recommended, use Global Resource files.

I would create one per xml file in the format of "filename.resx", so in your example, you'd name it Data.resx.

Set the "Name" in the resource to your "name" attribute and the Value equal to the translated "name".

For example, in Data.resx, you'd have Name=CategoryId, Value=Category Id. In Data.zh-CN.resx, you'd have Name=CategoryId, Value=类别.

One you have the data in the resource files, you'd probably want to create a class wraps the functionality of the XML lookup and localization for your in your application. Something like this should work:

public class Data
{
    private const string fileLocation = "TODO";

    public string Name{ get; set; }
    public string Value{ get; set; }

    private Data()
    {

    }

    public Data( string Name )
    {
        // TODO: Look up the single key from XML
    }

    public string GetLocalizedName( CultureInfo cultureInfo )
    {
        return Resources.Data.ResourceManager.GetString(Name, cultureInfo);
    }

    public static List<Data> LoadData()
    {
        List<Data> dataList = new List<Data>();

       // TODO: Load XML and create a list of Data objects.

        return dataList;
    }
}


Try the following steps:

Step 1 - Delete the temp files of your web site from

C:\Windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files

Step 2 - Clean and Rebuild your Solution.

Step 3 - Make sure that your Resource file in the App_LocalResources folder has the same name as the page that has this issue (and both App_LocalResources and the page are in the same folder).

Your App should be OK then.

http://iymbas.blogspot.com

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜