如何从web.config中读取system.web部分

时间:2022-11-06 20:21:24

Should be simple, but whatever I try returns null:

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

const string key = "system.web";

var sectionTry1 = WebConfigurationManager.GetSection(key);

var sectionTry2 = ConfigurationManager.GetSection(key);

I'm sure I have done this before.

我确定我以前做过这件事。

I am using MVC if this makes a difference.

如果这有所作为,我正在使用MVC。

3 个解决方案

#1


24  

Was being an idiot - system.web is not a config section but a config group. If I change the key to an actual section, then both methods work fine. Here's the one using ConfigurationManager:

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

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

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

#2


5  

I think accessing system.web is slightly different to accessing appSettings.

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

Try this:

尝试这个:

string configPath = "/MyAppRoot";

Configuration config = WebConfigurationManager.OpenWebConfiguration(configPath);

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

You need to cast the relevant section of system.web you're trying to access to a particular type.

你需要转换system.web的相关部分,你试图访问一个特定的类型。

#3


4  

This worked for me:

这对我有用:

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

#1


24  

Was being an idiot - system.web is not a config section but a config group. If I change the key to an actual section, then both methods work fine. Here's the one using ConfigurationManager:

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

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

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

#2


5  

I think accessing system.web is slightly different to accessing appSettings.

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

Try this:

尝试这个:

string configPath = "/MyAppRoot";

Configuration config = WebConfigurationManager.OpenWebConfiguration(configPath);

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

You need to cast the relevant section of system.web you're trying to access to a particular type.

你需要转换system.web的相关部分,你试图访问一个特定的类型。

#3


4  

This worked for me:

这对我有用:

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