TheTechGuy
TheTechGuy

Reputation: 17354

How to access local resource : MVC

I cam across this thread that ask how to access local resource and he solved his own problem but my situation is a little bit different.

Areas>Models>Support>Localization
                             MyResouces.resx
                     SupportModel

Inside my SupportModel I want to access values from Localization/MyResouces.resx. What is the syntax?

One solution is to use HttpContext.GetLocalResourceObjec(virtual path, key) but it has not worked for me. I have used

GetLocalResourceObject("~/Areas/Models/Support/SupportModel", "option1Text")

I also renamed the Localization folder to App_LocalResources but that did not solve the problem. May I am doing something wrong?

Note that I have modified my code as I did not want to post the real code. Thx

Upvotes: 0

Views: 4436

Answers (4)

Bonomi
Bonomi

Reputation: 2743

You can access it in the view using the following syntax:

@Resources.IndexPage.PropertyName

And you have to make sure your resource file has the following properties: Build Action: Embedded Resource Custom Tool: PublicResXFileCodeGenerator Custom Tool Namespace: Resources.IndexPage (This is a namespace given by you, it is used in the view to access properties.

Have a look at this article: http://www.chambaud.com/2013/02/27/localization-in-asp-net-mvc-4/

Upvotes: 0

TheTechGuy
TheTechGuy

Reputation: 17354

The solution I used is this. Interesting there is no solution available online.

using Myproject.Areas.Models.Support.Localization;
...
MyResouces.option1

The above is not actually compiled code.

Upvotes: 0

chridam
chridam

Reputation: 103365

To access values from Localization/MyResouces.resx, try this:

@Areas.Models.Support.Localization.MyResouces.option1Text

syntax: [namespace].[ResourceName].[Property]

provided that your MyResouces.resx file has public access modifier.

Upvotes: 1

VsMaX
VsMaX

Reputation: 1785

I have created an example application with Resource1.resx file inside Models/Support that's how I get the values from it:

public ActionResult Login(string returnUrl)
    {
        var res = Models.Support.Resource1.TestString1;  // this is resource created inside Models/Support/
        ViewBag.ReturnUrl = returnUrl;
        return View();
    }

Upvotes: 0

Related Questions