Localization, unit testing, and AJAX applications of ASP. net mvc applications (1)

Source: Internet
Author: User

BKJIA quick translation] Many web developers who use the traditional WebForm technology of ASP. NET are very interested in Microsoft's new open-source ASP. NET extension. This new extension is called ASP. net mvc. For software design, the concept of separating the user interface view, running logic controller, and data model has many advantages.

BKJIA editing recommendations :《ASP. net mvc video tutorial"

First, although this split requires a little more effort than usual, it helps to maintain the maintainability of the application. Another advantage is that it can better perform unit tests on your code. Framework and its Visual Studio built-in package) can help you create a unit test structure, for example, to create a unit test structure from a controller, and your task is to write the actual test code. ASP. net mvc 1.0 was just launched three months ago, so it takes time to get better applications in the industry. However, whether you use the traditional WebForm technology or ASP. net mvc, some requirements remain unchanged: You need to test, localize, and adjust the user interface details. In this article, we will learn how to use different methods for ASP. net mvc applications perform unit tests, how to localize multiple languages, and finally how to add AJAX touch to the user interface, where the user interface uses the view implementation in the MVC project.

Starting from localization

For Web applications, creating user interfaces in multiple languages is not a new problem. In fact, ASP. NET has built support for localization since the earliest version. For example, you may use local or global resources of the user and use the <%% %> structure in the control and/or. aspx files for reference. Views in ASP. net mvc applications are usually regular. aspx files, so you can use the same method as those in MVC applications. See figure 1 ).

Figure 1. Default appearance and layout of ASP. net mvc applications.

In addition to this traditional localization method, you can also add this logic to the Controller class. Since using <% $ %> is a boring method, you can use this structure to return different views from the Controller Based on the current language. This is very useful, especially when views of different language versions do not have to be consistent. For example, the English version of the view is better expanded than the French version.

Next, let's take a look at the two options in the actual operation. Suppose you have created a simple ASP. net mvc application and have not made any customization. The default application structure created by Visual Studio includes the Home and Account controllers. For example, if you want to localize the Home/About text, you can perform the following operations.

First, you can use the traditional localization method: use resources. For example, to use global resources, you need to add the App_GlobalResources folder to your solution. In Solution Explorer of Visual Studio, right-click the project node and choose Add/Add ASP. NET Folder/App_GlobalResources. In this way, you can create the required folder.

The next step is to add a resource file. In solution manager, right-click the newly created folder and choose Add/New Item from the pop-up menu. This will open the Add New Item dialog box and select the resource File Resources File) icon. After naming the resource file, click OK and Visual Studio will add it to your project. Then, you can type the resource string Graph 2 in the displayed table ). When using resources, you need to specify an identifier for each string localized in the application.

 

Figure 2. Use resource strings for localization in ASP. net mvc applications.


It is a good habit to specify strings that are easy to recognize before you start to use local applications, because you need these identifiers when trying to reference strings on the page. In addition, remember that many strings are unique for each page, so there may be multiple strings. You can use them in multiple views. Sometimes you need the identifier name to reflect this situation. Referencing a localized string is simple: you can use the <% $ Resources %> structure introduced in ASP. NET 2.0. For example, the MyResources. resx resource file contains a string named HelloWorldText. The following code in the. aspx file can be correctly converted to a localized string based on your preferred language:

 ﹤asp:Literal ID="Literal1" runat="server"
Text="﹤%$ Resources: MyResources, HelloWorldText %﹥" /﹥

For ASP. net mvc to find the correct localized version, the framework uses professional File naming rules. The MyResources. resx resource file contains strings of the relevant language, which is considered to be the initial language of the application. If your browser does not specify a language or you do not specify a language in the Code, the initial language string is used. However, if the selected language is German, "de" written in the first letter of the language will be added to the resource file name: MyResources.de. resx. The same applies to other languages. For example, the MyResources. es. resx file contains Spanish strings. For localization, you can also add special commands to the @ Page tag of the. aspx file. For example, to automatically detect the user's browser language settings, you can add the UICulture = "auto" attribute in the label after the colon in the default language:

﹤%@ Page ... UICulture="auto:en-US" %﹥

Another option for localization of a simple view is to return different views based on the user's language. Some logic algorithms need to be included in the controller, but the code is easy to write. For example, you can use the following logic:

public ActionResult Localized()
{
if (Request.UserLanguages.Length ﹥ 0)
{
string lang = Request.UserLanguages[0];
if (lang.ToUpper().StartsWith("DE"))
{
return View("Localized-DE");
}
else return View("Localized-EN");
}
else return View("Localized-EN");
}

You can also encapsulate the code in the Application) method. If you need to use both methods, the good news is that you can freely combine and match. This is exactly the case. The localization of ASP. net mvc applications should not be limited by the current available technology.


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.