Wednesday, April 6, 2011

Issue with GetLocalResource object

Hey SO:

I am trying to access my local resources file in my code-behind. I did some googling since I was unsure of how to do it and found this:

oContent.Text = HttpContext.GetLocalResourceObject("NonSupport").ToString();

However, I get an error saying that it needs at least two parameters: VirtualPath and ResourceKey. There is a third, CultureInfo but that one is optional. When I put this in as my virtual path:

HttpContext.GetLocalResourceObject("App_LocalResources/ExpandableListView.aspx.resx", "NonSupport").ToString();

I get the following compiler error message:

The relative virtual path 'App_LocalResources/ExpandableListView.aspx.resx' is not allowed here.

I must be doing something wrong with this since my searches (and some posts I found on here) say all I need to do is call the resource key.

Any thoughts? Thanks!

From stackoverflow
  • Did you put a resource file with the name (your aspx web page).aspx.resx into a App_LocalResource folder underneath the path where your ASPX page lives??

    Furthermore, just simply call the GetLocalResourceObject method on your current page:

    oContent.Text = GetLocalResourceObject("NonSupport").ToString();
    

    No need to use HttpContext for that - the method is defined on the Page class.

    Marc

    Anders : Yes. I added an asp.net folder (App_LocalResources) into the directory where this aspx file resides.
    marc_s : OK - did you use the Visual Studio "Add ASP.NET folder" method?
    Anders : Yep. Right clicked on the parent folder, selected "Add ASP.NET folder..." then selected the local resource one
    Anders : Thanks. The underlying problem was my class was declared static (dunno why it was there, maybe ReSharper added it) and the GetLocalResourceObject cannot be accessed inside of a static context.
    marc_s : OK, glad you found the culprit and a solution for it!

0 comments:

Post a Comment