获取ASP的参考.网网络.配置customErrors部分

本文关键字:配置 customErrors 部分 网络 ASP 参考 获取 | 更新日期: 2023-09-27 18:15:57

我试图获得对网络的参考。配置customErrors部分。当我使用下面的代码,我总是得到一个空。当我得到一个我创建的自定义部分的参考时,我没有这个问题,所以我有点目瞪口呆,为什么这不起作用。

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

我也试过这个:

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

我也试过这个:

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

啊!大多数事情都是这样,我问完问题就知道答案了。

这个对我有效:

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部分的引用,但我没有告诉它它所处的根部分,我的尝试是基于这样一个事实,即当我没有意识到我的自定义部分是该部分的根时,我知道如何获得自定义部分,所以我不必预先添加类似系统的东西。Web/在它前面,当我调用GetSection()

获取ASP的参考.网网络.配置customErrors部分

试试这个:

var configuration = WebConfigurationManager.OpenWebConfiguration("~/Web.config");
// Get the section.
CustomErrorsSection customErrors =
    (CustomErrorsSection)configuration.GetSection("system.web/customErrors");

更多的主题在这里:CustomError类