Asp.net 如何从用户特定的资源文件中提取标题?
我的网站项目中的Asp.net 如何从用户特定的资源文件中提取标题?,asp.net,vb.net,Asp.net,Vb.net,我的网站项目中的app\u GlobalResources文件夹下有两个资源文件(CaptionsA.resx和CaptionsB.resx),分别用于CustomerA和CustomerB 例如,在CaptionsA.resx中,我有: MyButtonText ------> Click me! MyButtonText ------> Click Here 在CaptionsB.resx中,我有: MyButtonText ------> Click me!
app\u GlobalResources
文件夹下有两个资源文件(CaptionsA.resx
和CaptionsB.resx
),分别用于CustomerA
和CustomerB
例如,在CaptionsA.resx
中,我有:
MyButtonText ------> Click me!
MyButtonText ------> Click Here
在CaptionsB.resx
中,我有:
MyButtonText ------> Click me!
MyButtonText ------> Click Here
我必须在我的网站的多个页面上使用标题。但是,当CustomerA
使用网站时,CaptionsA.resx
中的所有字幕都应可见,当CustomerB
使用网站时,CaptionsB.resx
中的所有字幕都应可见。请记住,两位客户都使用英语作为网站语言,因此我不能使用文化/语言本地化
我想问的是:
- 如何以编程方式告诉我的网站何时使用哪个资源文件李>
- 在我的VB.net代码中写什么李>
- 如何在我的代码中访问资源文件
If CustomerType = CustomerA //RETRIEVE DATA FROM CaptionsA.resx (How to do this?) else If CustomerType = CustomerB //RETRIEVE DATA FROM CaptionsB.resx (How to do this?)
aspx
源文件中写什么
<asp:Label ID="LblButtonText" runat="server" Text="<%$ Resources:**WHAT-TO-WRITE-HERE?**,MyButtonText %>"></asp:Label>
我已经搜索了很多,并试图在无数的论坛上找到答案,但与这个主题相关的帖子大多没有答案或没有帮助 以下是您的操作方法
Dim resourceFileBaseName As String = "WebApplicationNamespace.app_GlobalResources.CaptionsA"
Dim isCustomerB As Boolean = True
If isCustomerB Then
resourceFileBaseName = "WebApplicationNamespace.app_GlobalResources.CaptionsB"
End If
Dim customerBasedResourceManager = New System.Resources.ResourceManager(resourceFileBaseName, GetType(CaptionsA).Assembly)
Dim resourceManagerField = GetType(CaptionsA).GetField("resourceMan", BindingFlags.[Static] Or BindingFlags.NonPublic)
resourceManagerField.SetValue(Nothing, customerBasedResourceManager)
所有ResX文件生成一个等效类(例如CaptionsA),该类具有一个底层ResourceManager,该ResourceManager指向包含所有字符串的CaptionsA资源。根据客户类型,我们可以使此资源管理器指向正确的底层resx文件。但是这个资源管理器是类内部的,因此我们需要反映并设置值。另外,标题A和标题B彼此没有关系,否则我们可以利用一些模式/铸造来访问它们的成员
我们在上述代码中所做的是:
string resourceFileBaseName = "WebApplicationNamespace.app_GlobalResources.CaptionsA";
bool isCustomerB = true;
if (isCustomerB)
{
resourceFileBaseName = "WebApplicationNamespace.app_GlobalResources.CaptionsB";
}
var customerBasedResourceManager = new System.Resources.ResourceManager(resourceFileBaseName,
typeof(CaptionsA).Assembly);
var resourceManagerField = typeof(CaptionsA).GetField("resourceMan", BindingFlags.Static | BindingFlags.NonPublic);
resourceManagerField.SetValue(null, customerBasedResourceManager);
CaptionsA.MyButtonText将根据客户类型的resx文件指向该值