简体   繁体   English

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

[英]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. 我正在使用asp.net 3.5,我的解决方案目前有2个项目,一个API类项目和一个网站项目,在类项目中我有一个名为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. 为了能够从我的网站项目访问资源文件,我必须将“访问修饰符”设置为公共,这允许我使用强类型名称来访问资源,例如: 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. 我面临的问题是我无法从前端aspx代码访问资源,例如,设置标签的文本属性或验证错误消息。 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. 找不到具有键“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! 但无论我将类名设置为什么,我都会得到同样的错误,我正在考虑它,因为它试图查看网站项目,但我无法弄清楚如何告诉它看看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>

Resource expressions ( <%$ Resources: ClassKey, ResourceKey %> ) use ResourceExpressionBuilder class behind the scene. 资源表达式( <%$ Resources: ClassKey, ResourceKey %> )使用场景后面的ResourceExpressionBuilder类。 This class can lookup global and local resources only (in website's App_GlobalResources and App_LocalResources folders). 此类只能查找全局和本地资源(在网站的App_GlobalResourcesApp_LocalResources文件夹中)。

Instead, you can use CodeExpressionBuilder class to access resources from different project. 相反,您可以使用CodeExpressionBuilder类来访问来自不同项目的资源。 Here's how to use it. 这是如何使用它。

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 %>" />

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项目中的资源,从框架项目中的类库中获取资源 - 所以也许你会幸运地以相反的方式使用它。

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

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