检查Code中的CustomErrors已打开

是否可以在Web应用程序运行时的代码中检查天气自定义错误是打开还是关闭。

我已经想出如何做到这一点……

HttpContext.Current.IsCustomErrorEnabled

您可以使用WebConfigurationManager.OpenWebConfiguration获取网站的配置,然后使用它来获取自定义错误块:

Configuration configuration = WebConfigurationManager.OpenWebConfiguration(null); CustomErrorsSection customErrorsSection = configuration.GetSection("system.web/customErrors") as CustomErrorsSection; Response.Write(customErrorsSection.Mode.ToString()); 

OpenWebConfiguration(null)或HttpContext.Current.IsCustomErrorEnabled(true)给了我错误的信息,但这工作很棒复制粘贴:

 public static CustomErrorsMode GetRedirectMode() { Configuration config = WebConfigurationManager.OpenWebConfiguration("/"); return ((CustomErrorsSection)config.GetSection("system.web/customErrors")).Mode; }