繁体   English   中英

获取对ASP.NET web.config customErrors部分的引用

[英]get reference to the ASP.NET web.config customErrors section

我正在尝试获取对web.config customErrors部分的引用。 当我使用以下代码时,我总是得到一个null。 当我得到一个我创建的自定义部分的引用时,我没有这个问题,所以我有点傻眼为什么这不起作用。

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部分的引用,但是我没有告诉它它所居住的根部分,并且我的基础是我的尝试基于我知道如何当我没有意识到我的自定义部分是该部分的根时,得到一个自定义部分,所以当我调用GetSection()时,我不必在它前面添加类似system.Web /的东西。

试试这个:

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

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

有关此主题的更多信息: CustomError类

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM