简体   繁体   中英

How to return resource in asp.net view from method

I have some html code that I want to replace with text from resources.

My class looks like:

 public static class ResourceParser
    {
        public static string GetTextFromResource(string keyValue)
        {
            ResourceManager rm = new ResourceManager("pl", Assembly.GetExecutingAssembly());

            return rm.GetString(keyValue);
        }
    }

When I access resources from my view this way:

@Resources.pl.accept;

it works and displays the value I want.

When I do it like this:

@ResourceParser.GetTextFromResource("accept");

there is an exception

MissingManifestResourceException "Could not find any resources appropriate for the specified culture or the neutral culture. Make sure "Servers.Resources.resources" was correctly embedded or linked into assembly "myProject" at compile time, or that all the satellite assemblies required are loadable and fully signed."

It started working somehow.

In the meantime I've added this method:

 protected void Application_AcquireRequestState(object sender, EventArgs e)
        {
            if (Request.UserLanguages != null)
            {
                string culture = Request.UserLanguages[0];

                Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo(culture);
                Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(culture);
            }
        }

to my Global.asax.cs file

and I've change the code a bit:

public static string GetTextFromResource(string keyValue)
        {
            var path = "ProjectName.Folder.pl";

            var res_manager = new ResourceManager(path, typeof(pl).Assembly);

            return res_manager.GetString(keyValue);
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM