Get a link to an ASP.NET web.config customErrors section

I am trying to get a link to the web.config customErrors section. When I use the following code, I always get zero. I don't have this problem when I get a link to the user section that I created, so I'm a little overwhelmed by why this will not work.

CustomErrorsSection customErrorSection = ConfigurationManager.GetSection("customErrors") as CustomErrorsSection; 

I also tried this:

 CustomErrorsSection customErrorSection = WebConfigurationManager.GetSection("customErrors") as CustomErrorsSection; 

I also tried this:

 CustomErrorsSection customErrorSection = WebConfigurationManager.GetWebApplicationSection("customErrors") as CustomErrorSection; 

EDIT:

ARGH! This is the case with most of the things that I found out in response immediately after the question.

This works for me:

 System.Configuration.Configuration configuration = WebConfigurationManager.OpenWebConfiguration("/"); CustomErrorsSection customErrorsSection = (CustomErrorsSection)configuration.GetSection("system.web/customErrors"); 

Or just like that:

 CustomErrorsSection customErrors = (CustomErrorsSection) WebConfigurationManager.OpenWebConfiguration("/").GetSection("system.web/customErrors"); 

This also works:

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

So, I think, now I understand why I had the problem in the first place. I incorrectly thought that I could get a link to the customErrors section by trying to GetSection ("customErrors"), but I couldn’t say what root section it lived in, and I based my attempts on the fact that I knew how to get a custom section, when I didn’t understand that my user section was the root of the section, so I didn’t need to add something like system.Web / in front of it when I called GetSection ().

+4
source share
1 answer

Try the following:

 var configuration = WebConfigurationManager.OpenWebConfiguration("~/Web.config"); // Get the section. CustomErrorsSection customErrors = (CustomErrorsSection)configuration.GetSection("system.web/customErrors"); 

More on this here: CustomError class

+9
source

Source: https://habr.com/ru/post/1383073/


All Articles