无法在ASP.NET中使用C#代码访问.resx文件。 [英] Unable to access .resx file in C# code behind in ASP.NET.

查看:80
本文介绍了无法在ASP.NET中使用C#代码访问.resx文件。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用c#在asp.net中构建一个联系我们表单。联系表格必须以多种语言开发。因此,我决定将表单中的所有标签值移动到资源文件中,并尝试在后面的代码中访问它们。



例如



ASP.NET

------------

I am trying to build a contact us form in asp.net with c#. The contact form has to be developed across several languages. So, I have decided to move all the label values in the form to a resources file and trying to access them in the code behind.

For example

ASP.NET
------------

<div class="Container"  runat="server">
<div id="Phone"  runat="server"></div>
<asp:TextBox ID="TextBoxPhone" runat="server"></asp:TextBox>
</div>





C#-CODE BEHIND

---------

Phone.InnerHtml = Resources.ContactUs.Phone;



后面的代码无法在intellisence中加载Resources命名空间。它告诉资源不存在。任何人都可以帮忙吗?



C# -CODE BEHIND
---------
Phone.InnerHtml=Resources.ContactUs.Phone;

The code behind fails to load the Resources namespace in intellisence. It tells Resources does not exists. Can anybody help?

推荐答案

您可以通过两种方式获取资源(resx)值:



1.
You can get a resource (resx) value in two ways:

1.
ResourceFileName.YourKeyName



2.


2.

ResourceFileName.ResourceManager.GetString("YourKeyName");





如果您有不同语言的不同资源文件,我建议创建一个通用方法来获取基于文化或语言的值,而不是直接调用资源文件。





希望有帮助



Azee ...



If you have different resource files for different languages, I would suggest that create a common method to get the value based on the culture or language instead of directly calling a resource file.


Hope it helps

Azee...


解决了!将该应用程序作为一个网站。如果它是Web应用程序,我们需要通过上述注释中提到的密钥来访问它。谢谢。如果有人发布了关于如何在Web应用程序中访问的代码段,我将很高兴。 :)
Solved! Made the application as a website. If it is a web application, we need to access it through keys as mentioned in the above comment. Thanks. I will be happy if somebody posts a code snippet on how to access in web application. :)


在某些情况下,您可能希望从.resx文件中检索所有资源,而不是特定资源。为此,您可以使用System.Resources.ResXResourceReader类,该类为.resx文件中的所有资源提供枚举器。 System.Resources.ResXResourceReader类实现IDictionaryEnumerator,它返回一个DictionaryEntry对象,该对象表示循环的每次迭代的特定资源。它的DictionaryEntry.Key属性返回资源的键,其DictionaryEntry.Value属性返回资源的值。



以下示例为CarResources.resx文件创建ResXResourceReader对象在前面的示例中创建并遍历资源文件。它将资源文件中定义的两个Automobile对象添加到System.Collections.Generic.List(Of T)对象,并将六个字符串中的五个添加到SortedList对象。 SortedList对象中的值将转换为参数数组,该数组用于向控制台显示列标题。汽车属性值也会显示在控制台上。

C#

VB



Imports System.Collections

Imports System.Collections.Generic

Imports System.Resources



模块示例

Public Sub Main()

Dim resxFile As String =.\CarResources.resx

昏暗的汽车作为新列表(汽车)

Dim headers As New SortedList()



使用resxReader作为新的ResXResourceReader(resxFile)

For each entry as DictionaryEntry in resxReader

如果CType(entry.Key,String).StartsWith(EarlyAuto)那么

autos.Add(CType(entry.Value,Automobile))

否则如果CType(entry.Key,String).StartsWith(Header)则

headers.Add(CType(entry.Key,String),CType(entry.Value,String))

结束如果

下一页

结束使用

Dim headerColumns(headers.Count - 1)As String

headers.GetValueList()。CopyTo(headerColumns,0)

Console.WriteLine({0,-8} {1,-10} {2,-4} {3,-5} {4,-9},

headerColumns)

Console.WriteLine()

For Each auto In autos

Console.WriteLine({0,-8} {1,-10} {2,4} {3,5} {4,9},

auto.Make,auto.Model,auto.Year,

auto.Doors,auto.Cylinders)

下一页

结束次级

结束模块

'示例显示以下输出:

'制造型号年份门气缸

'

'福特型号N 1906 0 4

'Ford Model T 1909 2 4
In some cases, you may want to retrieve all resources, instead of a specific resource, from a .resx file. To do this, you can use the System.Resources.ResXResourceReader class, which provides an enumerator for all resources in the .resx file. The System.Resources.ResXResourceReader class implements IDictionaryEnumerator, which returns a DictionaryEntry object that represents a particular resource for each iteration of the loop. Its DictionaryEntry.Key property returns the resource's key, and its DictionaryEntry.Value property returns the resource's value.

The following example creates a ResXResourceReader object for the CarResources.resx file created in the previous example and iterates through the resource file. It adds the two Automobile objects that are defined in the resource file to a System.Collections.Generic.List(Of T) object, and it adds five of the six strings to a SortedList object. The values in the SortedList object are converted to a parameter array, which is used to display column headings to the console. The Automobile property values are also displayed to the console.
C#
VB

Imports System.Collections
Imports System.Collections.Generic
Imports System.Resources

Module Example
Public Sub Main()
Dim resxFile As String = ".\CarResources.resx"
Dim autos As New List(Of Automobile)
Dim headers As New SortedList()

Using resxReader As New ResXResourceReader(resxFile)
For Each entry As DictionaryEntry In resxReader
If CType(entry.Key, String).StartsWith("EarlyAuto") Then
autos.Add(CType(entry.Value, Automobile))
Else If CType(entry.Key, String).StartsWith("Header") Then
headers.Add(CType(entry.Key, String), CType(entry.Value, String))
End If
Next
End Using
Dim headerColumns(headers.Count - 1) As String
headers.GetValueList().CopyTo(headerColumns, 0)
Console.WriteLine("{0,-8} {1,-10} {2,-4} {3,-5} {4,-9}",
headerColumns)
Console.WriteLine()
For Each auto In autos
Console.WriteLine("{0,-8} {1,-10} {2,4} {3,5} {4,9}",
auto.Make, auto.Model, auto.Year,
auto.Doors, auto.Cylinders)
Next
End Sub
End Module
' The example displays the following output:
' Make Model Year Doors Cylinders
'
' Ford Model N 1906 0 4
' Ford Model T 1909 2 4


这篇关于无法在ASP.NET中使用C#代码访问.resx文件。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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