Global configuration of ASP. NET

Source: Internet
Author: User

Due to the project relationship, ASP. NET has been studying globalization issues over the past two days. I checked the information on the Internet. After some "hard work", it was a little small. I feel that there are no complete solutions on the Internet (or I may not find them), so I spent some time summing up my lessons and hope to share them with you. If you have any shortcomings, please kindly advise.
This article describes the basic steps and notes for implementing globalization in ASP. NET by creating an actual project in vs. net.
Step 1: create a web application named testrm. This project will include
-Assemblyinfo. CS
-Testrm. csproj
-Testrm. csproj. webinfo
-Testrm. sln
-Testrm. Suo
-Testrm. vsdisco
-Webform1.aspx (including Aspx. CS and Aspx. resx)
-Global. asax (including asax. CS and asax. resx)
-Web. config
Step 2: Create resource files in different languages for the project. In.. net, the resource file extension is. resx. This file is basically XML,. NET provides a very convenient resource file editing tool (I personally feel like xmlspy. Here, we use simplified Chinese (zh-CN) and American English (En-US) as examples. Use "Project-> Add new item-> resource file" to add strings to the project. en-us.resx and string. the resource file for the zh-cn.resx, and in the string. add a record in the en-us.resx with the content name: string001; Value: Welcome, in string. add a Record Name: string001; Value: Welcome to the zh-cn.resx. The XML file structure is as follows:
<? XML version = "1.0" encoding = "UTF-8"?>
<Root>
...
<Data name = "string001">
<Value> welcome </value>
</Data>
...
</Root>
If you do not want to change the language dynamically by modifying the configuration file, but want to change the language by setting the "region Settings" function of windows, skip step 3. (Is there such a demand ??? -_-)
In this way, after the project is compiled, two subdirectories, en-US and ZH-CN, are displayed under the bin directory, with a DLL file respectively. The name is testrm. Resources. dll.
Step 3: Modify the global. asax and web. config files to dynamically switch languages.
First, we need to add the following in the application_beginrequest event of Global. asax: Code :
Protected void application_beginrequest (Object sender, eventargs E)
{
Thread. currentthread. currentculture = cultureinfo. createspecificculture (configurationsettings. deleettings ["defaultculture"]);
}
This is the simplest application. Of course, you can store some configuration items in cookies.
Then, add the following code in Web. config:
<Deleetask>
<Add key = "defaultculture" value = "ZH-CN"/>
</Appsettings>
Note that the deleettings element and system. Web belong to the same level.
Finally, add the following code to the page_load event of webform1:
Public class webform1: system. Web. UI. Page
{
Private void page_load (Object sender, system. eventargs E)
{
Webform1 form1 = new webform1 ();
ResourceManager resmanager = new ResourceManager (form1.gettype (). namespace + ". String", form1.gettype (). Assembly );
System. Globalization. cultureinfo CI = system. Threading. thread. currentthread. currentculture;

Response. Write (resmanager. getstring ("string001", CI ));
}

...
}< br> Please note that the above Code contains the line of underline, in. net, if you use this. getType (). namespace and this. getType (). assembly. when fullname is obtained, it turns out to be ASP and 6fqpc_hh, version = 0.0.0.0, culture = neutral, publickeytoken = NULL. The obtained assembly is obviously randomly generated. But it is normal in C # class files and winform, and it is unclear why. Needless to say, the results certainly cannot find the corresponding resource file.
In addition, form1.gettype () is used here (). namespace + ". string ". If the resource file is not placed in the project root directory but in a subdirectory, modify the default namespace of the project. This is because after the resource file is compiled, it is stored in the project's resource file testrm. resources. the following line appears in the DLL manifest (viewed using ildasm ,. mresource public 'testrm. string. zh-CN.resources 'or. mresource public 'testrm. string. en-US.resources '. The basic composition rule is that the default project namespace + the directory name where the resource file is located + The resource file name [+ language]. If the resource file of a project is in no language, for example: we have defined a string. resx resource file. After the project is compiled, subdirectories such as ZH-CN and en-US will no longer be generated in the bin directory, the resource file will be directly embedded in the assembly of the project. At the same time, the manifest of the Project Assembly contains the following line. mresource public 'testrm. String. resources '. Therefore, if the resource file is not placed in the root directory of the project and the default namespace is not changed, you are trying to use resmanager. when getstring is used, the system will be prompted that "resource files in any language cannot be accessed" because it is described above.
basically, the above are some of my experiences in implementing globalization based on ASP. NET, hoping to help some friends.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.