14.15. Checking Out a Web Server's Custom Error Pages

Problem

You have an application that needs to know what custom error pages are set up for the various HTTP error return codes on a given IIS server.

Solution

Use the System.DirectoryServices.DirectoryEntry class to talk to the Internet Information Server (IIS) metabase to find out which custom error pages are set up. The metabase holds the configuration information for the web server. DirectoryEntry uses the Active Directory IIS service provider to communicate with the metabase by specifying the "IIS" scheme in the constructor for the DirectoryEntry:

	// This is a case-sensitive entry in the metabase.
	// You'd think it was misspelled, but you would be mistaken...
	const string WebServerSchema = "IIsWebServer";

	// Set up to talk to the local IIS server.
	string server = "localhost";

	// Create a dictionary entry for the IIS server with a fake
	// user and password. Credentials would have to be provided
	// if you are running as a regular user.
	using (DirectoryEntry w3svc =
	    new DirectoryEntry(
	        string.Format("IIS://{0}/w3svc", server),
	            "Domain/UserCode", "Password"))
	{

Once the connection is established, the web server schema entry is specified to show where the IIS settings are kept (IIsWebServer). The DirectoryEntry has a property that allows access to its children (Children), and the SchemaClassName is checked for each entry to see if it is in the web server settings section. Once the web server settings are found, the web root node is ...

Get C# 3.0 Cookbook, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.