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

发布于 2024-09-04 20:34:45 字数 259 浏览 1 评论 0原文

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

const string key = "system.web";

var sectionTry1 = WebConfigurationManager.GetSection(key);

var sectionTry2 = ConfigurationManager.GetSection(key);

我确信我以前已经这样做过。

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

Should be simple, but whatever I try returns 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

执手闯天涯 2024-09-11 20:34:45

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

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

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

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:

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

var outputCacheSettingsSection = ConfigurationManager.GetSection(outputCacheSettingsKey) as OutputCacheSettingsSection;
一杯敬自由 2024-09-11 20:34:45

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

试试这个:

string configPath = "/MyAppRoot";

Configuration config = WebConfigurationManager.OpenWebConfiguration(configPath);

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

您需要将您尝试访问的 system.web 的相关部分强制转换为特定类型。

I think accessing system.web is slightly different to accessing 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.

长不大的小祸害 2024-09-11 20:34:45

这对我有用:

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

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文