微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我正在扩展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,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()方法,无法保证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;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐