简体   繁体   English

如何在自定义服务器控件 asp 中使用 .resx 和 .resource 文件?

[英]How to use .resx and .resource files in custom server control asp?

I am writing my own server side control and I am using images that are being stored in a .resx file.我正在编写自己的服务器端控件,并且正在使用存储在.resx文件中的图像。 In the console application this code works fine:在控制台应用程序中,此代码工作正常:

     ResXResourceReader rsxr = new ResXResourceReader("Resource1.resx");
        
     foreach (DictionaryEntry d in rsxr)
     {
        Console.WriteLine(d.Key.ToString() + ":\t" + d.Value.ToString());
     }

     rsxr.Close();

but here但在这里

      protected override void RenderContents(HtmlTextWriter output)
    {
        ResXResourceReader rsxr = new ResXResourceReader("Resource1.resx");
  
        base.RenderContents(output);

        foreach (DictionaryEntry d in rsxr)
        {
            output.Write(d.Key.ToString());
        }
        
    }

I get this error:我收到此错误:

Could not find file 'C:\\Program Files\\Common Files\\Microsoft Shared\\DevServer\\10.0\\Resource1.resx'找不到文件“C:\\Program Files\\Common Files\\Microsoft Shared\\DevServer\\10.0\\Resource1.resx”

I tried to use the ResourceManager, but it requires a .resource file.我尝试使用 ResourceManager,但它需要一个.resource文件。 I can't access the resgen tool (command prompt does not understand the resgen command) and install it (during the attempt some errors ocured).我无法访问resgen工具(命令提示符不理解resgen命令)并安装它(在尝试期间出现一些错误)。

My questions are:我的问题是:

  1. Why can't I read .resx ?为什么我无法读取.resx

  2. How to install the resgen tool properly?如何正确安装resgen工具?

thanks.谢谢。

It is considered good practice to store your resource file under App_GlobalResources folder in application root or in App_LocalResources with the same name as your user control file.将资源文件存储在应用程序根目录中的App_GlobalResources文件夹下或与用户控制文件同名的App_LocalResources被认为是一种很好的做法。 So for example user user control is uc.ascx file in local resource folder should be uc.ascx.resx.因此,例如用户用户控件是本地资源文件夹中的 uc.ascx 文件应该是 uc.ascx.resx。 That way it is easier to maintain and asp.net will automatically detect it.这样维护起来更容易,而且asp.net 会自动检测到它。

Now your answers:现在你的答案是:

First Question: Use Server.MapPath("~/") points to physical directly where your web.config is.第一个问题:使用Server.MapPath("~/")直接指向 web.config 所在的物理位置。 If you want to use a resource file in Controls folder you have to write Server.MapPath("~/Controls/Resource1.resx") to get the path.如果要使用 Controls 文件夹中的资源文件,则必须编写Server.MapPath("~/Controls/Resource1.resx")以获取路径。

Not sure what you want to do with resgen tool?不确定您想用 resgen 工具做什么? When you compile your application, resource file will also be compiled.编译应用程序时,也会编译资源文件。 select your resource file and click F4, it will show you build action, choose resource in build action and your resource file will be included in assembly.选择您的资源文件并单击 F4,它将显示您的构建操作,在构建操作中选择资源,您的资源文件将包含在程序集中。

You can review this post for more information: How to use image resource in asp.net website?您可以查看此帖子以获取更多信息: 如何在 asp.net 网站中使用图像资源?

From your description, I understand you need to locate and access the user control's resource file.根据您的描述,我了解到您需要找到并访问用户控件的资源文件。 I found that it works nicely the following way:我发现它可以通过以下方式很好地工作:

  1. Create a App_GlobalResources on project level (via context menu Add -> Add ASP.NET Folder -> App_GlobalResources )在项目级别创建App_GlobalResources (通过上下文菜单Add -> Add ASP.NET Folder -> App_GlobalResources

  2. Create the ressource file with the same name as the control, but inside the App_GlobalResources .创建与控件同名的资源文件,但在App_GlobalResources 内 For example, if the control is named myControl.ascx , then the ressource file's name for the default language has to be myControl.ascx.resx例如,如果控件名为myControl.ascx ,则默认语言的资源文件名称必须为myControl.ascx.resx

  3. Create additional ressource files for each language you require.为您需要的每种语言创建额外的资源文件。 For instance, if you need German (" de-DE "), then add myControl.ascx.de.resx例如,如果您需要德语(“ de-DE ”),则添加 myControl.ascx.de.resx

  4. Add the class MultiLanguageUserControl as follows:添加类MultiLanguageUserControl如下:

     public class MultiLanguageUserControl : System.Web.UI.UserControl { public string getResValue(string id) { var ctrlPath = TemplateControl.AppRelativeVirtualPath; var ctrlFile = System.IO.Path.GetFileName(ctrlPath); var resObj = GetGlobalResourceObject(ctrlFile, id); if (resObj!=null) return resObj.ToString(); else return string.Format("UNRESOLVED[{0}]", id); } }
  5. Open the code behind of myControl and make it inherit from MultiLanguageUserControl instead from System.Web.UI.UserControl :打开 myControl 背后的代码并使其继承自MultiLanguageUserControl而不是System.Web.UI.UserControl

    public partial class myControl : MultiLanguageUserControl { //... }

  6. In the HTML code, use the new function, eg: <%=getResValue("resid")%> , where "resid" is the name of the ressource string you want to look up.在 HTML 代码中,使用新函数,例如: <%=getResValue("resid")%> ,其中"resid"是您要查找的资源字符串的名称。 You can also use the HTML-encoding tag <%: instead of <%= , depending on your requirements.您还可以使用 HTML 编码标记<%:代替<%= ,具体取决于您的要求。 Alternatively, you can use getResValue anywhere in your server-sided C# code in your user control to retrieve the value from the ressource file.或者,您可以在用户控件中服务器端 C# 代码的任何位置使用getResValue从资源文件中检索值。

  7. Ensure that you support the language detection in the Page_Load event of the page, which uses the user control.确保在使用用户控件的页面的Page_Load事件中支持语言检测。 How you can do this is described here (look for the function InitializeCulture ). 此处描述如何执行此操作(查找函数InitializeCulture )。

NOTE: If you want to read the page's local resource strings from inside the user control then take a look here .注意:如果您想从用户控件内部读取页面的本地资源字符串,请查看此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM