【问题标题】:How do I access resources file from a referenced library project?如何从引用的库项目中访问资源文件?
【发布时间】:2011-01-25 16:05:29
【问题描述】:

我有一个 ASP.Net 网站“MyApp”,其中包含以下资源文件:

  1. WebResources.resx
  2. WebResources.es.resx

该网站引用了一个库项目“MyLib”,我想从中访问这些资源文件。这是我正在尝试的代码:

var rm = new ResourceManager("MyApp", Assembly.GetExecutingAssembly());
subject = rm.GetString("HelloMessage"); //always string.empty

问题是执行程序集总是“MyLib”而不是“MyApp”。是否可以从库项目中访问网站项目中嵌入的资源文件?

感谢您的帮助, -基思

【问题讨论】:

    标签: asp.net embedded-resource class-library


    【解决方案1】:

    var res= HttpContext.GetGlobalResourceObject(resourceFile, resourceKey);

    将 resourceFile 替换为资源文件名(不带 .resx)。

    将 resourceKey 替换为键名。

    public static string GetResourceString(string resourceFile, string resourceKey)
    {
     if (HttpContext.Current == null)
         return resourceKey;
     var res = HttpContext.GetGlobalResourceObject(resourceFile, resourceKey);
     if (res == null)
     {
         // Log missing key here
         // ....
         res = resourceKey;
     }
     return res;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-25
      • 2023-03-22
      • 1970-01-01
      • 2014-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多