Как прочитать раздел system.web из web.config - PullRequest
14 голосов
/ 11 июня 2010

Должно быть простым, но все, что я пытаюсь, возвращает нуль:

const string key = "system.web";

var sectionTry1 = WebConfigurationManager.GetSection(key);

var sectionTry2 = ConfigurationManager.GetSection(key);

Я уверен, что делал это раньше.

Я использую MVC, если это имеет значение.

Ответы [ 3 ]

28 голосов
/ 11 июня 2010

Был идиотом - system.web - это не раздел конфигурации, а группа конфигурации. Если я изменю ключ на фактический раздел, то оба метода работают нормально. Вот тот, который использует ConfigurationManager:

const string outputCacheSettingsKey = "system.web/caching/outputCacheSettings";           

var outputCacheSettingsSection = ConfigurationManager.GetSection(outputCacheSettingsKey) as OutputCacheSettingsSection;
7 голосов
/ 11 июня 2010

Я думаю, что доступ к system.web немного отличается от доступа к appSettings.

Попробуйте это:

string configPath = "/MyAppRoot";

Configuration config = WebConfigurationManager.OpenWebConfiguration(configPath);

IdentitySection section = (IdentitySection)config.GetSection("system.web/identity");

Вам нужно привести соответствующий раздел system.web, который вы пытаетесьдоступ к определенному типу.

5 голосов
/ 28 августа 2012

Это сработало для меня:

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