14

単純なはずですが、私が試したものはすべてnullを返します。

const string key = "system.web";

var sectionTry1 = WebConfigurationManager.GetSection(key);

var sectionTry2 = ConfigurationManager.GetSection(key);

私は以前にこれを行ったことがあると確信しています。

これが違いを生むのであれば、私はMVCを使用しています。

4

3 に答える 3

28

ばかだった-system.webは構成セクションではなく、構成グループです。キーを実際のセクションに変更すると、どちらの方法でも問題なく機能します。これがConfigurationManagerを使用しているものです。

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

var outputCacheSettingsSection = ConfigurationManager.GetSection(outputCacheSettingsKey) as OutputCacheSettingsSection;
于 2010-06-11T11:35:24.960 に答える
7

system.webへのアクセスは、appSettingsへのアクセスとは少し異なると思います。

これを試して:

string configPath = "/MyAppRoot";

Configuration config = WebConfigurationManager.OpenWebConfiguration(configPath);

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

特定のタイプにアクセスしようとしているsystem.webの関連セクションをキャストする必要があります。

于 2010-06-11T10:14:28.863 に答える
6

これは私のために働いた:

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);
}
于 2012-08-28T16:44:12.477 に答える