Link to home
Start Free TrialLog in
Avatar of xmichaelm
xmichaelm

asked on

HOWTO: Programatically Configure Error Pages Features Settings in IIS 7.0

I would like to programmatically (C#) configure IIS 7.0 for a given application, to do the equivalent of manually:

Computer Management => Services and Applications => Internet Information Services => Sites => Default Web Site => ApplicationName => Error Pages
Clicking "Edit Feature Setttings..."
Under "Error Reponses" set "When the server encounter an error, return:" to "Custom error pages".

Thanks,
Michael
ASKER CERTIFIED SOLUTION
Avatar of Meir Rivkin
Meir Rivkin
Flag of Israel image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of xmichaelm
xmichaelm

ASKER

Thanks sedgwick!

I tried to adapt the sample code you have posted to configure a single application (as I requested in my question above), but I get the following exception:

System.Reflection.TargetInvocationException: Exception has been thrown by the ta
rget of an invocation. ---> System.IO.FileLoadException: This configuration sect
ion cannot be used at this path. This happens when the section is locked at a pa
rent level. Locking is either by default (overrideModeDefault="Deny"), or set ex
plicitly by a location tag with overrideMode="Deny" or the legacy allowOverride=
"false".

See source code below.

Thanks,
Michael
using (ServerManager serverManager = new ServerManager()) {
    Microsoft.Web.Administration.Configuration config = serverManager.GetWebConfiguration("Default Web Site", "/MediaServerWeb");
    Microsoft.Web.Administration.ConfigurationSection httpErrorsSection = config.GetSection("system.webServer/httpErrors");
    httpErrorsSection.Attributes["errorMode"].Value = 1; // "Custom";
    serverManager.CommitChanges();
}

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial