代码之家  ›  专栏  ›  技术社区  ›  Paul Hiles

如何从web.config读取system.web节

  •  14
  • Paul Hiles  · 技术社区  · 15 年前

    应该很简单,但是无论我尝试什么,都返回空值:

    const string key = "system.web";
    
    var sectionTry1 = WebConfigurationManager.GetSection(key);
    
    var sectionTry2 = ConfigurationManager.GetSection(key);
    

    我肯定我以前做过。

    如果这有什么区别的话,我使用的是MVC。

    3 回复  |  直到 7 年前
        1
  •  27
  •   Paul Hiles    15 年前

    是一个白痴-system.web不是一个配置部分,而是一个配置组。如果我将键更改为实际的部分,那么这两种方法都可以正常工作。下面是使用配置管理器的:

    const string outputCacheSettingsKey = "system.web/caching/outputCacheSettings";           
    
    var outputCacheSettingsSection = ConfigurationManager.GetSection(outputCacheSettingsKey) as OutputCacheSettingsSection;
    
        2
  •  7
  •   Hakan Fıstık MuriloKunze    7 年前

    我认为访问system.web与访问appsettings略有不同。

    试试这个:

    string configPath = "/MyAppRoot";
    
    Configuration config = WebConfigurationManager.OpenWebConfiguration(configPath);
    
    IdentitySection section = (IdentitySection)config.GetSection("system.web/identity");
    

    您需要强制转换System.Web中试图访问特定类型的相关部分。

        3
  •  5
  •   Mihai Iorga    13 年前

    这对我很有用:

    public Int32 GetmaxRequestLength()
    {
        // Set the maximum file size for uploads in bytes.
        var section = ConfigurationManager.GetSection("system.web/httpRuntime") as HttpRuntimeSection;
        // return length converted to kbytes or return default value as specified
        return (Int32) Math.Round((decimal)(section != null ? (double)section.MaxRequestLength * 1024 / 1000 : 5.120), 2);
    }