Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access resx resource files from another project

I'm using asp.net 3.5, my solution currently has 2 projects, an API class project and a website project, within the class project I have a resource file named checkin.resx. For me to be able to access the resource files from my website project, I had to set the "Access Modifier" to public, this allowed me to use a strongly typed name to acces the resources for example: CkiApi.Checkin.Resources.Checkin.OCKI_HeaderText, where Checkin is the .resx file and OCKI_HeaderText is the resource key.

The problem I'm facing is that I'm unable to access the resources from front end aspx code, for example, setting a text property of a label or a validation error message. I have tried the following syntax to no avail:

<asp:Label AssociatedControlID="IdentMethods" EnableViewState="false" ID="lblIdentMethod" runat="server" Text="<%$ Resources: CkiApi.Checkin.Resources.Checkin, OCKI_IdentificationMethod %>"></asp:Label>

the error I get is

The resource object with key 'OCKI_IdentificationMethod' was not found.

but regardless of what I set the class name to, I get the same error, I'm thinking its because its trying to look in the website project but I can't figure out how to tell it to look at the API! Can anyone help?

I am able to set non server side tags using the following:

<div id="OckiIntroText">
    <%=CkiApi.Checkin.Resources.Checkin.OCKI_IntroText%>
</div>
like image 905
Raj Avatar asked Aug 03 '09 13:08

Raj


People also ask

How do I open resources in Resx?

resx file in the XML (Text) editor. Press Alt+\ or choose ReSharper | Navigate | Go to File Member… from the main menu . Alternatively, you can press Ctrl+Shift+A , start typing the command name in the popup, and then choose it there. You will see the list of resource entries in a popup.

How do I read a RESX file?

Create an App_GlobalResources system folder and add a resource file to it e.g. Messages. resx. Create your entries in the resource file e.g. ErrorMsg = This is an error. Then to access that entry: string errormsg = Resources.

Are RESX files compiled?

resx files are compiled into binary . resources files.

Do RESX files need to be deployed?

You do not need to deploy . resx file with your application.


2 Answers

Resource expressions (<%$ Resources: ClassKey, ResourceKey %>) use ResourceExpressionBuilder class behind the scene. This class can lookup global and local resources only (in website's App_GlobalResources and App_LocalResources folders).

Instead, you can use CodeExpressionBuilder class to access resources from different project. Here's how to use it.

Add CodeExpressionBuilder class to App_Code folder:

using System.CodeDom;
using System.Web.Compilation;
using System.Web.UI;

[ExpressionPrefix("Code")]
public class CodeExpressionBuilder : ExpressionBuilder
{
   public override CodeExpression GetCodeExpression(BoundPropertyEntry entry,
      object parsedData, ExpressionBuilderContext context)
   {
      return new CodeSnippetExpression(entry.Expression);
   }
}

Add the following to system.web/compilation section in web.config:

<compilation debug="false">
   ...
   <expressionBuilders>
      <add expressionPrefix="Code" type="CodeExpressionBuilder"/>
   </expressionBuilders>
</compilation>

Finally, you can call into strongly-typed class generated for your .resx file:

<asp:Label ID="Label1" runat="server" Text="<%$ Code: ClassLibrary1.Resource1.String1 %>" />
like image 189
Pavel Chuchuva Avatar answered Sep 17 '22 14:09

Pavel Chuchuva


Not sure if this will solve your problem but have you looked at the HttpContext.GetGlobalResourceObject method?

I've used it to access resources in the web project, from class libraries in a framework project - so perhaps you will have luck in using it the other way around.

like image 33
Winston Smith Avatar answered Sep 18 '22 14:09

Winston Smith