You are developing an ASP.NET MVC application in Visual Studio 2012. The application supports multiple
cultures. The application contains three resource files in the Resources directory:
My Dictionary.resx
MyDictionary.es.resx
MyDictionary.fr.resx
Each file contains a public resource named Title with localized translation. The application is configured to set
the culture based on the client browser settings. The application contains a controller with the action defined in
the following code segment. (Line numbers are included for reference only.)
You need to set ViewBag.Title to the localized title contained in the resource files. Which code segment should
you add to the action at line 03?
A.
ViewBag.Title = HttpContext.GetGlobalResourceObuect(“MyDictionary”, “Title”);
B.
ViewBag.Title = HttpContext.GetGlobalResourceObject(“MyDictionary”, “Title”, new System.Globalization.
CultureInfo(“en”));
C.
ViewBag.Title = Resources.MyDictionary.Title;
D.
ViewBag.Title = HttpContext.GetLocalResourceObject(“MyDictionary”, “Title”);
GetLocalResourceObject only takes one parameter, so D is not correct.
Answer is A. Global setting at application level: http://asp.net-tutorials.com/localization/local-and-global-resources/
a
A. Has a typo in method name “GetGlobalResourceOb>u<ect".
Besides that, the default resource file's name is "My Dictionary.resx" (with space). That eliminates C. as Resources.MyDictionary is not generated, instead you get Resources.My_Dictionary.
If we use A. without a typo, we get the expected result if locale is not default. Otherwise you get an exception (because of space in default resource file).
Without a space in resource file, both A. and C. provide desired results.
So… The question is incorrect?
GetGlobalResourceObject returns a object, so its necessary add .ToString() to the ViewBag. There’s no answers with .ToString(), so C it’s more correct.
Answer is C. Because the MyDictionary.resx is in Resources folder not App_GlobalResources. In order to access it via GetGlobalResourceObject it must be under App_GlobalResources folder. Hope the file name “My Dictionary.resx” (with space) is a typo. Even if it is really with space C makes more correct answer. Only thing is if there user’s culture doesn’t match es and fr, the Title is going to be null.