开发者

HttpContext.GetGlobalResourceObject Always null from Class Library

I have a resx file in App_GlobalResources in my web application, called with:

Resources.GetResource("ResourceFileName", "Resource")

The helper method lives 开发者_StackOverflowin a separate class library to get resource values:

using System.Resources;
using System.Web;

public static class Resources
{
    public static string GetResource(string resource, string key)
    {
        try
        {
            string resourceValue = (string)HttpContext.GetGlobalResourceObject(resource, key);

            return string.IsNullOrEmpty(resourceValue) ? string.Empty : resourceValue;
        }
        catch (MissingManifestResourceException)
        {
            return string.Empty;
        }
    }
}

If I hit F5, everything works fine. If I deploy to a web server, all calls to GetGlobalResourceObject come back as null.

The resources exist. How do I get them out?

Thanks,

Richard


I have same problem too and yes "GetGlobalResourceObject(...)" return always null after publish.

So, I found the solution by using instead of 'GetGlobalResourceObject("Captions","button_go_text")' to 'Resources.Captions.ResourceManager.GetString(key)'

Sample code is like below


    public static string GetResMsg(this HtmlHelper htmlHelper, string key)
    {
      try
      {
        return Resources.[resource class name].ResourceManager.GetString(key);
      }
      catch
      {
        return "?";
      }
    }

I hope that helps,

best regards


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

HttpContext.GetGlobalResourceObject Always null from Class Library

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

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜