从另一个项目访问resx资源文件

时间:2020-12-21 15:03:44

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.

我正在使用asp.net 3.5,我的解决方案目前有2个项目,一个API类项目和一个网站项目,在类项目中我有一个名为checkin.resx的资源文件。为了能够从我的网站项目访问资源文件,我不得不将“访问修饰符”设置为公共,这允许我使用强类型名称来访问资源,例如:CkiApi.Checkin.Resources.Checkin .OCKI_HeaderText,其中Checkin是.resx文件,OCKI_HeaderText是资源键。

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:

我面临的问题是我无法从前端aspx代码访问资源,例如,设置标签的文本属性或验证错误消息。我尝试了以下语法无济于事:

<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.

找不到具有键“OCKI_IdentificationMethod”的资源对象。

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?

但无论我将类名设置为什么,我都会得到同样的错误,我正在考虑它,因为它试图查看网站项目,但我无法弄清楚如何告诉它看看API!有人可以帮忙吗?

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

我可以使用以下设置非服务器端标签:

<div id="OckiIntroText">
    <%=CkiApi.Checkin.Resources.Checkin.OCKI_IntroText%>
</div>

2 个解决方案

#1


17  

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).

资源表达式(<%$ Resources:ClassKey,ResourceKey%>)使用场景后面的ResourceExpressionBuilder类。此类只能查找全局和本地资源(在网站的App_GlobalResources和App_LocalResources文件夹中)。

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

相反,您可以使用CodeExpressionBuilder类来访问来自不同项目的资源。以下是如何使用它。

Add CodeExpressionBuilder class to App_Code folder:

将CodeExpressionBuilder类添加到App_Code文件夹:

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:

将以下内容添加到web.config中的system.web / compilation部分:

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

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

最后,您可以调用为.resx文件生成的强类型类:

<asp:Label ID="Label1" runat="server" Text="<%$ Code: ClassLibrary1.Resource1.String1 %>" />

#2


3  

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

不确定这是否会解决您的问题,但您是否看过HttpContext.GetGlobalResourceObject方法?

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.

我已经用它来访问Web项目中的资源,从框架项目中的类库中获取资源 - 所以也许你可以幸运地使用它来反过来。

#1


17  

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).

资源表达式(<%$ Resources:ClassKey,ResourceKey%>)使用场景后面的ResourceExpressionBuilder类。此类只能查找全局和本地资源(在网站的App_GlobalResources和App_LocalResources文件夹中)。

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

相反,您可以使用CodeExpressionBuilder类来访问来自不同项目的资源。以下是如何使用它。

Add CodeExpressionBuilder class to App_Code folder:

将CodeExpressionBuilder类添加到App_Code文件夹:

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:

将以下内容添加到web.config中的system.web / compilation部分:

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

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

最后,您可以调用为.resx文件生成的强类型类:

<asp:Label ID="Label1" runat="server" Text="<%$ Code: ClassLibrary1.Resource1.String1 %>" />

#2


3  

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

不确定这是否会解决您的问题,但您是否看过HttpContext.GetGlobalResourceObject方法?

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.

我已经用它来访问Web项目中的资源,从框架项目中的类库中获取资源 - 所以也许你可以幸运地使用它来反过来。