How to use app_GlobalResource or app_LocalResource?
Asked Answered
E

3

38

How to use them in a ASP.NET Web Application project? Any difference?

many thanks

Evident answered 7/7, 2010 at 10:50 Comment(0)
M
32

Local Resources:

  1. Local resource is specific to a single Web page and used for providing versions of a Web page in different languages.
  2. Local resources must be stored in App_LocalResources sub folder.
  3. Local resources must be named in format [.language / language and culture].resx.

Ex: Default.aspx.resx- Base resource file. This is the default, or fallback, resource file.

Default.aspx.de.resx- A resource file for German etc.

Global Resources:

Global Resources Localization Suggestion for ASP.NET

  1. Global resource can be read from any page or code that is in the application.
  2. Global resource must be stored in App_GlobalResources at the root of the application.

    // Get the global resource string. 
    
     try
     { 
         globalresourcestring = (String)GetGlobalResourceObject("MyResource", "GlobalResourceString1"); 
     } 
     catch
     { 
         globalresourcestring = "Could not find global resource."; 
     }
    
Markhor answered 7/7, 2010 at 10:56 Comment(3)
how call global resource from c#?Evident
tnks parany, I have also used by this way too: Resources.GeneralMessages.msg where msg is the name of that resource.Evident
Shorter notation: globalresourcestring = GetGlobalResourceObject("MyResource", "GlobalResourceString1") as string ?? "Could not find global resource.";Anatol
A
8

These are the steps for ASP.NET MVC 5 Web Application

  1. Right-click the solution name.
  2. Add > Add ASP.Net Folder > App_LocalResources
  3. File should open. Add content. (Name/Value)
  4. Locate Access Modifier at the top of the .resx file and change it to Public and save the file.
  5. Right click the .resx file of choice and select properties.
  6. Change Build Action from content to embedded Resource.
  7. Open solution explorer and expand the Views folder.
  8. Open the web.config file.
  9. Add new namespace to the namespaces section.

ex. <add namespace="ProjectName.App_LocalResources" />

In order to access the local resource file in your html code you must add

@using ProjectName.App_LocalResources

to the top of the page.

You can then access the different resources in the App_LocalResources by using the

@NameOfResxFile.NameOfValueInResxFile

I hope this helps.

Austroasiatic answered 19/1, 2014 at 6:47 Comment(1)
how can I use the resources in the Model ?Noseband
S
1

If you are so sure about the fact that your resource exists on your .resx file, you can try :

string myValue = HttpContext.GetGlobalResourceObject("MyResourceFile","MyResource").ToString();

If you are not sure whether your resource exists or not you can use :

var resultMessage = HttpContext.GetGlobalResourceObject("MyResourceFile","MyResource");

string myValue = resultMessage == null ? string.Empty : resultMessage.ToString();
Splenius answered 20/1, 2017 at 11:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.