HttpContext.GetGlobalResourceObject returns null but the value is available through Resources

StackOverflow https://stackoverflow.com/questions/23274965

  •  09-07-2023
  •  | 
  •  

Вопрос

I'm trying to access values from a resx resource file using HttpContext.GetGlobalResourceObject.

The resource file is in a folder called Resources and has the CustomTool property set to PublicResXFileCodeGenerator as per K. Scott Allen: http://odetocode.com/blogs/scott/archive/2009/07/16/resource-files-and-asp-net-mvc-projects.aspx

The resources are being built and embedded (I can see then in the assembly via ILSpy) and I can access the values in code using:

var b = MyApp.Resources.GlobalStrings.VendorUITitle;

But trying to access them via GetGlobalResourceObject always returns null

var s = HttpContext.GetGlobalResourceObject("MyApp.Resources.GlobalStrings", "VendorUITitle");
Это было полезно?

Решение

I was able to solve this problem by setting the Custom Tool of my resource file to be GlobalResourceProxyGenerator

And the Build Action to Content and Copy to Output Directory property to Copy Always

Resource File Properties

If I didn't do this then when I published to production calls to HttpContext.GetGlobalResourceObject would always return null.

Другие советы

  1. Create resource file under App_GlobalResources
  2. Set Build Action to Content
  3. Set Copy to Output Directory to Do not copy
  4. Ensure you have App_GlobalResources folder in deployed aplication
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top