title: Check httpErrors errorMode programmatically
body: <p>How do I get the value of the errorMode property set in the <code>&lt;system.webServer&gt;&lt;httpErrors&gt;</code> element in web.config?</p>
  
  <p>I'm trying to implement some "self-diagnostics" in an ASP.NET web application. When the app starts, it runs through some of the settings in web.config and confirm they're set correctly.</p>
  
  <p>While this code works quite nicely when the errormode is set in the <code>&lt;system.web&gt;&lt;customErrors&gt;</code> element,</p>
  
  <pre><code>var errSec = (CustomErrorsSection)HttpContext.Current.GetSection("system.web/customErrors");
  Response.Write(errSec.Mode.ToString());
  </code></pre>
  
  <p>it won't work once the site is deployed on IIS7 and this setting is now found in <code>system.webServer -&gt; httpErrors</code>. </p>
  
  <p>This won't work:</p>
  
  <pre><code>var errSec = (CustomErrorsSection)HttpContext.Current.GetSection("system.webServer/httpErrors");
  </code></pre>
  
  <p>And casting to a <code>CustomErrorsSection</code> also seems like a bad idea, there must be a better type to use?</p>
  
  <p>I found this article on IIS.NET, <a href="http://www.iis.net/ConfigReference/system.webServer/httpErrors" rel="noreferrer">HTTP Errors  </a>, but I hope to do this without the dependency on the Microsoft.Web.Administration library.</p>
  
  <p>Any suggestions??</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Okay, based on the suggestion below, I tried this: </p>
  
  <pre><code>var errSec = (ConfigurationSection)HttpContext.Current.GetSection("system.webServer/httpErrors");
  Response.Write(errSec.SectionInformation.GetRawXml().ToString());
  </code></pre>
  
  <p>But that doesn't work either, the <code>errSec</code> object is null. And on a side-note, if I load the <code>&lt;system.web&gt;&lt;customErrors&gt;</code> section using the same approach, the <code>GetRawXml()</code> method call fails with a "This operation does not apply at runtime." exception message.</p>
  
  <p>I know how to load the whole web.config as an xml file and query that to get to the element I need. But it just seems to me like there must be a more elegant approach.</p>
  
  <p>How to read web.config as xml: </p>
  
  <pre><code>var conf = XDocument.Load(System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath + "web.config");
  var errMode = conf.Root.Element("system.webServer").Element("httpErrors").Attribute("errorMode").Value;
  </code></pre>
  
  <p>... but that's just nasty! And if the errorMode setting is set in machine.config or similar, it won't work.</p>