22

Using ASP.NET MVC Razor, I have a resource file in App_GlobalResources named General.resx and General.fr.resx with a name value pairings of "Hello" and "Hello" and "Hello" and "Bonjour".

How do I reference these from my view?

These do not work:

@Html.Resource("General.Hello")
@Html.Resource("GlobalResources.Hello")
@Html.Resource("GlobalResources.General.Hello")
@Html.Resource("GlobalResources, General.Hello") 
Stacked
  • 6,892
  • 7
  • 57
  • 73
FiveTools
  • 5,970
  • 15
  • 62
  • 84
  • 1
    If you resources files are not showing in the view then you need to do this https://holyhoehle.wordpress.com/2010/02/20/making-global-resources-public/ – Saad A Oct 18 '17 at 16:28

3 Answers3

46

Try this,

@Resources.General.Hello

syntax: Resources.[ResourceName].[Property]

VJAI
  • 32,167
  • 23
  • 102
  • 164
  • 1
    I knew it would be something simple. Thanks! – FiveTools Jul 09 '12 at 17:31
  • 1
    @FiveTools I don't understand your comment. If you all want is globalization in your mvc app this is the best resource i can give you http://adamyan.blogspot.in/2010/02/aspnet-mvc-2-localization-complete.html. – VJAI Jul 09 '12 at 19:31
  • 5
    @Dismissile You should not tell blankly *i would strongly suggest against this approach*. You should include the reason why you discourage it, so it will help the OP and others (by the way I'm truly aware of the problems of using App_GlobalResources/App_LocalResources in MVC apps). – VJAI Jul 09 '12 at 19:34
  • Sorry - Mark - I was responding to dismissile - I believe your approach to be the easiest and best solution. – FiveTools Jul 09 '12 at 20:00
  • 1
    @Dismissile If I have to use this resource in 15 different views I have to create 15 different changes to existing models, and then when another developer revisits the source 6 mths from now to change 'hello' to 'goodbye' they will need to make a change in the controller and the view. KISS. Am I mistaken? – FiveTools Jul 09 '12 at 20:00
1

To access value from resoure file in view

Add this name space

@using System

Then display the value like this

@ResourceFile.Address  //ResouceFile is the name of the ResouceFile

This method is used when evironment culture thread is used.

Arun Prasad E S
  • 9,489
  • 8
  • 74
  • 87
  • 2
    **From review queue**: May I request you to please add some context around your source-code. Code-only answers are difficult to understand. It will help the asker and future readers both if you can add more information in your post. – RBT Jan 05 '18 at 06:24
0

you need to refer to your namespace:

@GeneralResxNamespace.General.Hello

You can do this in an easier way by adding namespace to /Views/Web.config file In section with namespaces add section below:

<add namespace="*YourProjectNamespace*.*ResourcesNamespace*.Resources" />

Then you can use resources without namespace, like in the example:

General.Hello
WholeLifeLearner
  • 455
  • 4
  • 19