Как читать раздел system.web из web.config

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

const string key = "system.web";

var sectionTry1 = WebConfigurationManager.GetSection(key);

var sectionTry2 = ConfigurationManager.GetSection(key);

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

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

Ответы

Ответ 1

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

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

var outputCacheSettingsSection = ConfigurationManager.GetSection(outputCacheSettingsKey) as OutputCacheSettingsSection;

Ответ 2

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

Попробуйте следующее:

 string configPath = "/MyAppRoot";

        Configuration config = WebConfigurationManager.OpenWebConfiguration(configPath);

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

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

Ответ 3

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

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