Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

HOWTO: Programatically Configure Error Pages Features Settings in IIS 7.0

Posted on 2009-12-27
4
Medium Priority
?
1,482 Views
Last Modified: 2013-12-17
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
0
Comment
Question by:xmichaelm
  • 3
4 Comments
 
LVL 42

Accepted Solution

by:
Meir Rivkin earned 2000 total points
ID: 26129877
u can custom your error pages using ServerManager (Microsoft.Web.Administration.dll)
checkout the code snippet.
take a look at http://www.iis.net/ConfigReference/system.webServer/httpErrors/error for further reading.
using System;
using System.Text;
using Microsoft.Web.Administration;

internal static class Sample
{
   private static void Main()
   {
      using (ServerManager serverManager = new ServerManager())
      {
         Configuration config = serverManager.GetApplicationHostConfiguration();
         ConfigurationSection httpErrorsSection = config.GetSection("system.webServer/httpErrors");
         ConfigurationElementCollection httpErrorsCollection = httpErrorsSection.GetCollection();

         ConfigurationElement errorElement = httpErrorsCollection.CreateElement("error");
         errorElement["statusCode"] = 404;
         errorElement["subStatusCode"] = 5;
         errorElement["prefixLanguageFilePath"] = @"%SystemDrive%\inetpub\custerr";
         errorElement["path"] = @"404.5.htm";
         httpErrorsCollection.Add(errorElement);

         serverManager.CommitChanges();
      }
   }
}

Open in new window

0
 
LVL 1

Author Comment

by:xmichaelm
ID: 26130358
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

0
 
LVL 42

Assisted Solution

by:Meir Rivkin
Meir Rivkin earned 2000 total points
ID: 26130395
this is a security percaution done by the iis, u need to unlock the relevant entry in the iis configuration file first.

u can unlock it using appcmd.exe or via IIS Manager.

appcmd exmaples:

//for all sites
appcmd unlock config section:httpErrors  

//for Default Web Site only
appcmd unlock config "Default Web Site" section:httpErrors

C# code snippet:
const string APPCMD_PATH = @"C:\Windows\System32\inetsrv\appcmd.exe";

Process proc = Process.Start(APPCMD_PATH, "unlock config -section:httpErrors");
proc.WaitForExit();
if(proc.ExitCode != 0)
{
//handle appcmd failure
}

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();
}

//unlock the httpErrors section once done configurating
Process proc = Process.Start(APPCMD_PATH, "lock config -section:httpErrors");
proc.WaitForExit();
if(proc.ExitCode != 0)
{
//handle appcmd failure
}
0
 
LVL 42

Expert Comment

by:Meir Rivkin
ID: 26130407
check http://learn.iis.net/page.aspx/155/an-overview-of-feature-delegation-in-iis-70/ for further reading about IIS configuration
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you don't have the right permissions set for your WordPress location in IIS, you won't be able to perform automatic updates. Here's how to fix the problem.
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Screencast - Getting to Know the Pipeline
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question