C# 检查在代码中打开的自定义错误

C# 检查在代码中打开的自定义错误,c#,exception-handling,C#,Exception Handling,是否可以检查web应用程序运行时上的代码中是否打开或关闭了自定义错误 您可以使用获取网站的配置,然后使用该配置获取自定义错误块: Configuration configuration = WebConfigurationManager.OpenWebConfiguration(null); CustomErrorsSection customErrorsSection = configuration.GetSection("system.web/customErrors")

是否可以检查web应用程序运行时上的代码中是否打开或关闭了自定义错误

您可以使用获取网站的配置,然后使用该配置获取自定义错误块:

Configuration configuration =
    WebConfigurationManager.OpenWebConfiguration(null);

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

Response.Write(customErrorsSection.Mode.ToString());

我已经知道怎么做了它在

HttpContext.Current.IsCustomErrorEnabled

OpenWebConfiguration(null)或HttpContext.Current.IsCustomErrorEnabled(true)给了我错误的信息,但是效果很好 复制粘贴:

public static CustomErrorsMode GetRedirectMode()
{
    Configuration config = WebConfigurationManager.OpenWebConfiguration("/");

    return ((CustomErrorsSection)config.GetSection("system.web/customErrors")).Mode;
}
OpenWebConfiguration(null)
似乎没有在当前应用程序中加载web.config,而是在计算机上安装了web.config。其他答案显示了OpenWebConfiguration(“~”),但我还没有确认。