检查Code中的CustomErrors已打开

时间:2009-09-02 13:16:15

标签: c# exception-handling

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

3 个答案:

答案 0 :(得分:15)

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

HttpContext.Current.IsCustomErrorEnabled

答案 1 :(得分:3)

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

Configuration configuration =
    WebConfigurationManager.OpenWebConfiguration(null);

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

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

答案 2 :(得分:0)

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

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

    return ((CustomErrorsSection)config.GetSection("system.web/customErrors")).Mode;
}
相关问题