简体   繁体   English

检查Code中的CustomErrors已打开

[英]Checking CustomErrors turned on in Code

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

I've figured out how to do it it's in... 我已经弄清楚如何做到这一点......

HttpContext.Current.IsCustomErrorEnabled HttpContext.Current.IsCustomErrorEnabled

You can use WebConfigurationManager.OpenWebConfiguration to obtain the configuration for the website, then use that to get the custom errors block: 您可以使用WebConfigurationManager.OpenWebConfiguration获取网站的配置,然后使用它来获取自定义错误块:

Configuration configuration =
    WebConfigurationManager.OpenWebConfiguration(null);

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

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

OpenWebConfiguration(null) or HttpContext.Current.IsCustomErrorEnabled(true) gave me false information, this however worked great Copy paste: OpenWebConfiguration(null)或HttpContext.Current.IsCustomErrorEnabled(true)给了我错误的信息,但这工作很棒复制粘贴:

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

    return ((CustomErrorsSection)config.GetSection("system.web/customErrors")).Mode;
}

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

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