如何在不重新编译的情况下动态加载和切换web应用程序(ASP.NET)中的资源文件?

如何在不重新编译的情况下动态加载和切换web应用程序(ASP.NET)中的资源文件?,asp.net,resources,Asp.net,Resources,我希望将web应用程序的资源文件(包含标签文本等)存储在数据库中,以便以后能够动态编辑和创建它们(可能在UI中)。我的想法是将整个resx文件存储在一个xml列中,然后根据当前用户登录应用程序或切换上下文时的语言和其他属性按需加载。重要的是,资源不仅依赖于用户的文化信息,而且还依赖于一些上下文信息,这些信息可以由用户切换,而无需关闭或再次打开(在我们的示例中称为“组”,与一组用户无关) 是否可以从外部源加载资源的内容,并在服务器不重新编译web应用程序的情况下简单地切换它们?我知道我可以在一些接

我希望将web应用程序的资源文件(包含标签文本等)存储在数据库中,以便以后能够动态编辑和创建它们(可能在UI中)。我的想法是将整个resx文件存储在一个xml列中,然后根据当前用户登录应用程序或切换上下文时的语言和其他属性按需加载。重要的是,资源不仅依赖于用户的文化信息,而且还依赖于一些上下文信息,这些信息可以由用户切换,而无需关闭或再次打开(在我们的示例中称为“组”,与一组用户无关)


是否可以从外部源加载资源的内容,并在服务器不重新编译web应用程序的情况下简单地切换它们?我知道我可以在一些接口上连接并实现一个自定义资源提供程序,它可以直接从数据库中读取数据,但是如果它能够以某种方式与resx文件一起工作,它可能会使事情变得更简单。

很晚了,但因为到目前为止还没有答案

System.Resources.ResourceReader resourceReader 
    = new System.Resources.ResourceReader("PathToResourceFile");
差不多就是这样。现在,您可以创建en.resx或de.resx之类的资源文件,并根据用户语言加载它们。差不多

System.Resources.ResourceReader resourceReader 
    = new System.Resources.ResourceReader(HttpContext.Current.Request.UserLanguages[0] 
    + ".resource");
请记住,为用户提供默认语言(资源文件)时使用的语言是您不支持的

编辑:


看看这个。

确保您可以轻松地完成这项工作,并且它适用于直接的XML文件。您不需要使用resx文件

/// <summary>
/// Sets or replaces the ResourceDictionary by dynamically loading
/// a Localization ResourceDictionary from the file path passed in.
/// </summary>
/// <param name="resourceDictionaryFile">The resource dictionary to use to set/replace
/// the ResourceDictionary.</param>
private void SetCultureResourceDictionary(String resourceDictionaryFile)
{
    // Scan all resource dictionaries and remove, if it is string resource distionary
    for ( int index= 0; index < Resources.MergedDictionaries.Count; ++index)
    {
        // Look for an indicator in the resource file that indicates the resource is
        // swappable. For instance in our files the header contains this: 
        // <sys:String x:Key="ResourceDictionaryName">Resources-en-CA</sys:String> 
        if (Resources.MergedDictionaries[index].Contains("ResourceDictionaryName"))
        {
            if ( File.Exists(resourceDictionaryFile) )
            {
                Resources.MergedDictionaries.Remove(Resources.MergedDictionaries[index]);
            }
        }
    }

    // read required resource file to resource dictionary and add to MergedDictionaries collection
    ResourceDictionary newResourceDictionary =  new ResourceDictionary();
    newResourceDictionary.Source = new Uri(resourceDictionaryFile);
    Resources.MergedDictionaries.Add(newResourceDictionary);
}
//
///通过动态加载设置或替换ResourceDictionary
///来自传入文件路径的本地化ResourceDictionary。
/// 
///用于设置/替换的资源字典
///资源字典。
私有void SetCultureResourceDictionary(字符串resourceDictionaryFile)
{
//扫描所有资源字典并删除(如果是字符串资源字典)
for(int index=0;index
这个问题已经6岁了,但我还是要回答:)

要读取.resx文件,您需要使用System.Windows.Forms.dll中的
System.Resources.ResXResourceReader

这是很好的解释。只是一个完整性的快速示例:

using (ResXResourceReader resxReader = new ResXResourceReader(@".\CarResources.resx"))
{
    foreach (DictionaryEntry entry in resxReader) {
        // ...
    } 
}

你有什么解决办法吗?我也面临同样的情况。谢谢我也在寻找这个问题的答案。为了澄清这一点,我只处理编译过的资源文件,例如.resource,而不是XML文件,例如.resx,对吗?