简体   繁体   English

如何从方法返回asp.net视图中的资源

[英]How to return resource in asp.net view from method

I have some html code that I want to replace with text from resources. 我有一些html代码,我想用资源中的文本替换。

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." MissingManifestResourceException“找不到适合于指定区域性或中性区域性的任何资源。确保在编译时已将” Servers.Resources.resources“正确嵌入或链接到程序集” myProject“中,或者所有所需的附属程序集都可加载并完全签名。”

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 到我的Global.asax.cs文件

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);
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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