4

I'm trying to obtain a reference to the web.config customErrors section. When I use the following code I always get a null. I don't have this problem when I get a reference to a custom section that I've created so I'm a bit dumbfounded why this won't work.

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

I've also tried this:

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

I've also tried this:

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

EDIT:

ARGH! Such is the case with most things I figured out the answer right after asking the question.

This works for me:

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

Or more simply like this:

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

This also works:

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

So I guess I understand now why I had the problem in the first place. I had incorrectly thought that I could get a reference to the customErrors section by trying to GetSection("customErrors") but I had failed to tell it what root section it lived in and I was basing my attempts on the fact that I knew how to get a custom section when I failed to realize that my custom section was the root of the section so I did not have to prepend something like system.Web/ in front of it when I called GetSection().

3
  • Perhaps a silly question, but does the section exist in your config file? Commented Nov 25, 2011 at 14:11
  • 1
    Legitimate and could help others. Don't delete it. Commented Nov 25, 2011 at 14:26
  • Thanks! I was dumbfounded because getting the customError section was different than getting a custom section. I figured it'd be the same. Commented Nov 25, 2011 at 14:30

1 Answer 1

8

Try this:

var configuration = WebConfigurationManager.OpenWebConfiguration("~/Web.config");

// Get the section.
CustomErrorsSection customErrors =
    (CustomErrorsSection)configuration.GetSection("system.web/customErrors");

More on the subject here: CustomError Class

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks! I spent a while searching this out and I went back to the Microsoft page right after posting and I found out that I hadn't scrolled down far enough the first couple times I hit the page. Sometimes you get so frustrated trying to find an answer that you can't think clearly enough to interpret the material that you are searching for.
I'll mark your answer as the correct one but your code snippet only shows part of the solution that you linked to. You didn't paste the part that declares where configuration comes from.
configuration = WebConfigurationManager.OpenWebConfiguration("~/Web.config"); CustomErrorsSection
@Darren Thanks for adding the missing part! :) Updated the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.