4

web.configcustomErrorsセクションへの参照を取得しようとしています。次のコードを使用すると、常にnullが発生します。作成したカスタムセクションへの参照を取得してもこの問題は発生しないため、これが機能しない理由には少し戸惑っています。

CustomErrorsSection customErrorSection =
    ConfigurationManager.GetSection("customErrors") as CustomErrorsSection;

私もこれを試しました:

CustomErrorsSection customErrorSection = 
    WebConfigurationManager.GetSection("customErrors") as CustomErrorsSection;

私もこれを試しました:

CustomErrorsSection customErrorSection =
    WebConfigurationManager.GetWebApplicationSection("customErrors") as CustomErrorSection;

編集:

ARGH!これは、質問をした直後に答えを見つけたほとんどの場合に当てはまります。

これは私のために働きます:

System.Configuration.Configuration configuration = WebConfigurationManager.OpenWebConfiguration("/");
CustomErrorsSection customErrorsSection = (CustomErrorsSection)configuration.GetSection("system.web/customErrors");

またはもっと簡単にこのように:

CustomErrorsSection customErrors = (CustomErrorsSection) WebConfigurationManager.OpenWebConfiguration("/").GetSection("system.web/customErrors");

これも機能します:

CustomErrorsSection customErrorsSection = ConfigurationManager.GetSection("system.web/customErrors") as CustomErrorsSection;

だから、そもそもなぜ問題があったのか理解できたと思います。GetSection( "customErrors")を試行することでcustomErrorsセクションへの参照を取得できると誤って考えていましたが、それがどのルートセクションにあるかを伝えることができず、その方法を知っているという事実に基づいて試行していました。カスタムセクションがセクションのルートであることに気づかなかったときにカスタムセクションを取得したので、GetSection()を呼び出したときにその前にsystem.Web/のようなものを追加する必要はありませんでした。

4

1 に答える 1

8

これを試して:

var configuration = WebConfigurationManager.OpenWebConfiguration("~/Web.config");

// Get the section.
CustomErrorsSection customErrors =
    (CustomErrorsSection)configuration.GetSection("system.web/customErrors");

詳細はこちら: CustomError クラス

于 2011-11-25T14:11:54.967 に答える