How to revert an HttpError ConfigurationElement back to "Inherited" using IIS 7 API

joshcomley picture joshcomley · Nov 23, 2010 · Viewed 11.6k times · Source

Suppose I have the following <httpErrors> collection in a web.config:

<httpErrors>
</httpErrors>

Yep, nice 'n empty.

And in IIS 7, my HTTP errors page looks like this:

httpErrors

Beautiful! (I've highlighted 404 simply because that's the example I'll use in a sec).

Now, I run the following code:

errorElement["statusCode"] = 404;
errorElement["subStatusCode"] = -1;
errorElement["path"] = "/404.html";
httpErrorsCollection.Add(errorElement);

Lovely. I now have, as expected this in my web.config:

<httpErrors>
    <remove statusCode="404" subStatusCode="-1" />
    <error statusCode="404" subStatusCode="-1" prefixLanguageFilePath="" path="/404.html" />
</httpErrors>

Couldn't be happier. Now, in IIS 7 my HTTP errors section looks, as expected, like below: alt text

Life couldn't be sweeter at this point. Now, down the line, I want to programmatically revert my 404 error back to the state shown in the original screenshot. Logic dictates that I should remove my new error:

httpErrorsCollection.Remove(errorElement);

But alas, if I do this, my web.config looks a lot like this:

    <httpErrors>
        <remove statusCode="404" subStatusCode="-1" />
    </httpErrors>

And my IIS looks a bit like this:

alt text

This is expected because of my web.config - but how, using ServerManager and all it's useful IIS 7 API, do I remove the httpError element entirely and revert my web.config back to:

<httpErrors>
</httpErrors>

Any ideas?

Answer

sushma picture sushma · Dec 17, 2012

In IIS7 and above under Management section, we can see an icon named Configuration Editor, double click and expand to

system.webserver-->webdav-->httpErrors

Right click on Default path, under

section-->Click on Revert to Parent

Then restart the website

The changes will be reverted back