应该很简单,但无论我尝试什么都返回 null:
const string key = "system.web";
var sectionTry1 = WebConfigurationManager.GetSection(key);
var sectionTry2 = ConfigurationManager.GetSection(key);
我确定我以前做过。
如果这有所作为,我正在使用 MVC。
应该很简单,但无论我尝试什么都返回 null:
const string key = "system.web";
var sectionTry1 = WebConfigurationManager.GetSection(key);
var sectionTry2 = ConfigurationManager.GetSection(key);
我确定我以前做过。
如果这有所作为,我正在使用 MVC。
是个白痴-system.web 不是配置部分,而是配置组。如果我将密钥更改为实际部分,那么这两种方法都可以正常工作。这是使用 ConfigurationManager 的一个:
const string outputCacheSettingsKey = "system.web/caching/outputCacheSettings";
var outputCacheSettingsSection = ConfigurationManager.GetSection(outputCacheSettingsKey) as OutputCacheSettingsSection;
我认为访问 system.web 与访问 appSettings 略有不同。
尝试这个:
string configPath = "/MyAppRoot";
Configuration config = WebConfigurationManager.OpenWebConfiguration(configPath);
IdentitySection section = (IdentitySection)config.GetSection("system.web/identity");
您需要将您尝试访问的 system.web 的相关部分转换为特定类型。
这对我有用:
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);
}