共享Web应用程序之间的asp.net资源文件 [英] Sharing asp.net resource files between web applications

查看:95
本文介绍了共享Web应用程序之间的asp.net资源文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有需要分享(的.resx)有人建议将资源文件移动到一个单独的程序,并有web项目引用它的资源文件的多个项目。是否有一个如何做到这一点的例子吗?

I have multiple projects that need to share resource files (.resx) Suggestions have been made to move resource files to a separate assembly and have web projects reference it. Is there an example of how to do this?

我创建一个新的类库项目并移动的里面App_GlobalResource文件夹?我不认为这会工作,因为那些资源文件生成code类被标记为内部,这意味着他们无法在此集会的外部访问。

Do I create a new Class Library project and move App_GlobalResource folder inside of that? I don't think that will work because code classes that are generated for resource files are marked as 'internal' which means that they can not be accessed outside of this assembly.

推荐答案

在Visual Studio中的属性窗口中,你应该能够对资源文件的访问修饰符设置为公开。你不会,但是,可以使用正常的&LT访问资源ASPX文件;%$资​​源:... ...%GT; 语法,因为它没有在引用的程序集的支持资源。我有同样的问题,并通过实现自定义前pressionBuilder解决了这个问题,但我没有在目前访问我的源$ C ​​$ C。如果它仍然需要的话,我可以看看今晚的code。

In the properties window of Visual Studio, you should be able to set the access modifier of the resource file to public. You won't, however, be able to access the resources in aspx files using the normal <%$ Resources:... %> syntax, since it does not support resources in referenced assemblies. I had the same problem and solved it by implementing a custom ExpressionBuilder, but I don't have access to my source code at the moment. If it's still needed then, I can look up the code tonight.

编辑:好的,这是我如何解决了这个问题:

OK, here's how I solved that problem:

第1步:将RESX文件到类库。它们并不需要在一个特定文件夹。在RESX文件的可视化设计,设置访问修饰符(上右上角)为公共

Step 1: Move the resx files into the class library. They do not need to be in a specific folder. In the visual designer of the resx file, set the "Access Modifier" (upper right-hand corner) to "Public"

您现在应该能够


  • 引用的资源的 C#/ VB code 的(在库,以及在Web项目),例如,昏暗myMessage的String = [命名空间] Resources.NameOfResx.NameOfResource

  • reference the resources in C#/VB code (in the library as well as in the web project), e.g., Dim myMessage As String = [Namespace.]Resources.NameOfResx.NameOfResource

引用资源在aspx页面,如&LT直列code; H1&GT;&LT;%= [命名空间] Resources.NameOfResx.MyTitle%GT;&LT; / H1方式&gt;

reference the resource as inline code in aspx pages, e.g., <h1><%= [Namespace.]Resources.NameOfResx.MyTitle %></h1>.

什么的不会的在这一点上的工作是利用资源EX pression,例如,&LT; ASP:按钮=服务器文本=&LT ;%$资源:NameOfResx,MyButtonText%GT; /&GT; 。不幸的是,你不能简单地用内联code取代这个,因为它是一个服务器端控件的属性里面。

What won't work at this point is to use Resources expression, e.g., <asp:Button runat="server" Text="<%$ Resources:NameOfResx,MyButtonText %>" />. Unfortunately, you cannot simply replace this with inline code, since it's inside the property of a server side control.

第2步:让我们在我们的图书馆定制防爆pressionBuilder,其中除$ P $点任意code EX pressions。幸运的是,我们可以让.NET Framework的强大阶级做所有的工作,为我们:

Step 2: Let's make a custom ExpressionBuilder in our library, which interprets arbitrary code expressions. Fortunately, we can let the powerful classes of the .net Framework do all the work for us:

Imports System.Web.Compilation
Imports System.Resources
Imports System.CodeDom

<ExpressionPrefix("Code")> _
Public Class CodeExpressionBuilder
    Inherits ExpressionBuilder

    Public Overrides Function GetCodeExpression(ByVal entry As System.Web.UI.BoundPropertyEntry, ByVal parsedData As Object, ByVal context As System.Web.Compilation.ExpressionBuilderContext) As System.CodeDom.CodeExpression
        Return New CodeSnippetExpression(entry.Expression)
    End Function
End Class

然后,我们需要注册在web.config此防爆pressionBuilder:

Then we need to register this ExpressionBuilder in the web.config:

<system.web>
  ...
  <compilation ...>
    <expressionBuilders>
      <add expressionPrefix="Code" type="NamespaceOfYourLibrary.CodeExpressionBuilder" />
    </expressionBuilders>
  </compilation>
</system.web>

现在,你应该能够做到以下几点:

Now you should be able to do the following:

<asp:Button runat="server" Text="<%$ Code:[Namespace.]Resources.NameOfResx.MyButtonText %>" />

信贷:我从<一的codeEX pressionBuilder的想法href=\"http://weblogs.asp.net/infinitiesloop/archive/2006/08/09/The-$c$cEx$p$pssionBuilder.aspx\">Infinites环路博客。如果你更到C#比VB,你可以看一下code例子有:

Credit: I got the idea for the CodeExpressionBuilder from the Infinites Loop blog. If you're more into C# than VB, you can have a look at the code examples there.

这篇关于共享Web应用程序之间的asp.net资源文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆