c# - 扩展 ASP.NET 资源提供程序

标签 c# .net asp.net .net-4.0

我正在扩展 asp.net 资源提供程序。 问题是扩展资源提供程序没有检测到页面文化。 GetObject() 方法中的 CultureInfo 始终为 null。

要更改页面文化,我正在使用一个覆盖 InitializeCulture() 的列表框:

protected override void InitializeCulture()
        {
            if (Request.Form["ListBox1"] != null)
            {
                String selectedLanguage = Request.Form["ListBox1"];
                UICulture = selectedLanguage;
                Culture = selectedLanguage;
                Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(selectedLanguage);
                Thread.CurrentThread.CurrentUICulture = new CultureInfo(selectedLanguage);
            }
            base.InitializeCulture();
        }

还有 GetObject() 函数:

public object GetObject(string resourceKey, CultureInfo culture)
        {
            //Call of the data access layer function to retreave the resource value from the database
            string resourceValue = m_dalc.GetResourceByCultureAndKey(culture, resourceKey);

            return resourceValue;
        }

这是资源提供者类:

public class DBResourceProvider : IResourceProvider
    {
        #region local variables

        //We save the classKey (resourceType) in this variable
        private string m_classKey;

        //New instance of the data access layer
        private StringResourcesDALC m_dalc;

        #endregion

        #region Constructors

        /// <summary>
        /// Constructor that creates a new instance of a resource provider using the resource type
        /// </summary>
        /// <param name="classKey">Resource type</param>
        public DBResourceProvider(string classKey)
        {
            this.m_classKey = classKey;
            m_dalc = new StringResourcesDALC(classKey);
        }

        #endregion

        #region IResourceProvider Members

        /// <summary>
        /// Function that is called when we have explicit declarations of local and global resources
        /// </summary>
        /// <param name="resourceKey">Key of the resource</param>
        /// <param name="culture">Culture code</param>
        /// <returns>Resource value</returns>
        public object GetObject(string resourceKey, CultureInfo culture)
        {
            //Call of the data access layer function to retreave the resource value from the database
            string resourceValue = m_dalc.GetResourceByCultureAndKey(culture, resourceKey);

            return resourceValue;
        }

        //Property that returns a new resource reader used to get local resources wich have been declared implicitly
        public System.Resources.IResourceReader ResourceReader
        {
            get
            {
                //Call of the data access layer function that returns all resource keys and values for a single culture
                ListDictionary resourceDictionary = this.m_dalc.GetResourcesByCulture(CultureInfo.InvariantCulture);

                return new DBResourceReader(resourceDictionary);
            }

        }

        #endregion

    }

非常感谢您的宝贵时间。

最佳答案

对于GetObject()方法,不保证当前的culture是ASP.NET提供的。尝试以下操作:

public object GetObject(string resourceKey, CultureInfo culture) {
    if (culture == null)
    {
        culture = CultureInfo.CurrentUICulture;
    }

    //Call of the data access layer function to retreave the resource value from the database
    string resourceValue = m_dalc.GetResourceByCultureAndKey(culture, resourceKey);
    return resourceValue;
} 

关于c# - 扩展 ASP.NET 资源提供程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4647104/

相关文章:

c# - ASP.Net Listview 空 ItemTemplate 不显示

c# - 如何为 ListBoxItems 设置不同的 Horizo​​ntalAlignment

c# - 如何向 ScintillaNet 添加新的语言设置?

.net - VB.NET 如何从 SQL 查询的文本中获取结果

c# - 在不使用 JObject 的情况下使用 Json.net 反序列化派生类

c# - .NET 在异步任务完成之前返回响应

javascript - $ ("123").ready (fn(){}) 无论如何都会触发

c# - 无法在 asp.net core "No service for ISession"中使用 session 已注册

c# - Azure 自定义 Controller /API .Net 后端

c# - 在参数中声明 IDisposable 时如何处理它?