Checking CustomErrors turned on in Code

不羁岁月 提交于 2019-12-23 08:07:21

问题


Is it possible to check weather custom errors is turned on or off in the code on web application runtime.


回答1:


I've figured out how to do it it's in...

HttpContext.Current.IsCustomErrorEnabled




回答2:


You can use WebConfigurationManager.OpenWebConfiguration to obtain the configuration for the website, then use that to get the custom errors block:

Configuration configuration =
    WebConfigurationManager.OpenWebConfiguration(null);

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

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



回答3:


OpenWebConfiguration(null) or HttpContext.Current.IsCustomErrorEnabled(true) gave me false information, this however worked great Copy paste:

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

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


来源:https://stackoverflow.com/questions/1367640/checking-customerrors-turned-on-in-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!