Programmatically Change HTTPErrors in IIS6

Have you ever needed to setup IIS Custom Errors file path(HTTP Errors file path) in IIS6 programmatically?   If yes, this post will give you an idea how you can set that up.

We do have “website stand-up” web application what it does is it creates website, application pool, set website related properties like host header, custom errors file path(404 and 500 error code), etc..  We also have ability to set language to website by setting up culture/UICulture in web.config.  Another cool thing is that we do have web farm setup each web farm has three web server so you can think that how easy to set up website.  Just one click  will setup website in all different web server.  Isn’t it cool?  I think it’s good idea to show you all steps how you can do that but at this point I will only focus on how you can setup custom errors file path programmatically.

Here we go step by step…

1) Connecting to IIS6

IIS metabase contains many objects that can be accessed or manipulated by ASP.NET.  The metabase is the database used to house all IIS centric information.  For more information about IIS metabase, you can look at this MSDN article.

We will use IIS://LOCALHOST/W3SVC interface to access IIS centric information.  There are three pieces in that interface.

  • IIS:  It indicates that we are accessing IIS Services
  • LOCALHOST: It’s name of the machine whose IIS service we are going to access.  It could be IP address if you want to setup custom errors path on remote IIS server.  In our case we will setup on local machine thats why we used LOCALHOST.
  • W3SVC: It indicates we are accessing world wise web services

We got enough information to connect IIS metabase object so let’s get IIS object.

DirectoryEntry w3svc = new DirectoryEntry(String.Format("IIS://{0}/W3SVC",ipAddress),username,password);
//you need to supply username and password that has sufficient previllege to access IIS metabase

so here we got IIS server level object “w3svc”.  Let’s get website object that we are looking for to set up HTTPErrors path.

IIS://{0}/W3SVC/{1} : This interface will give you the web site object

IIS://{0}/W3SVC/{1}/Root : This interface will give you the root of the web site object.  If you have virtual directory underneath website, you can get that virtual directory object using IIS://{0}/W3SVC/{1}/Root/Virtual_Directory_Name

{0} = ipAddress
{1} = website identifier (it’s uniqueid assigned to each website)

Here is the code to get the root of the website object.

DirectoryEntry siteRootDE = new DirectoryEntry(string.Format("IIS://{0}/W3SVC/{1}/Root", ipAddress, siteID), userName, password);

There is a “Properties” property of DirectoryEntry object that returns list of website properties like HTTPErrors, Path, AppFriendlyName, AppPoolID that you can set. In our case siteRootDE.Properties[“HTTPErrors”] will return PropertyValueCollection object that will have all HTTPError code.

For example,

siteRootDE.Properties[“HTTPErrors”][0] will return “”400,*,FILE,C:\WINDOWS\help\iisHelp\common\400.htm” string.  It contains four different values separated by commas.

400: Custom Error Code
*: Sub Error Code
FILE: It’s a Message Type. Other possible values are Default, URL.
CustomError File Path.

You can loop through siteRootDE.Properties[“HTTPErrors”] collection and find the custom error code that you want to update the file path and simply assign new value to that property.

At the end you need to call CommitChanges method of DirectoryEntry object to commit all the changes that you have made.

Here is the complete code to set HTTPErrors in IIS 6.0


string ipAddress = "10.1.1.111";  //ipAddress of machine whose IIS we are going to access
string username = "shailesh";
string password = "patel";
//you need to supply username and password that has sufficient previllege to access IIS metabase

DirectoryEntry w3svc = new DirectoryEntry(String.Format("IIS://{0}/W3SVC",ipAddress),username,password);
DirectoryEntry siteRootDE = new DirectoryEntry(string.Format("IIS://{0}/W3SVC/{1}/Root", ipAddress, siteID), username, password);

PropertyValueCollection httpErrorLists = siteRootDE.Properties["HTTPErrors"];

string[] iisHTTPError;
string iisError_SubErrorCode = string.Empty;
string updated_404_HttpError = "404,*,FILE,\UNCSharedCustomErrors404.es-MX.htm";
bool isChanged = false;

for (int i = 0; i < httpErrorLists.Count; i++)
{
 iisHTTPError = httpErrorLists[i].ToString().Split(",".ToCharArray());
 iisError_SubErrorCode = iisHTTPError[0] + "," + iisHTTPError[1];

 if (iisError_SubErrorCode == "404,*")
 {
 siteRootDE.Properties["HttpErrors"][i] = updated_404_HttpError ;

 //I think it's good practice to check whether the file path we are going to set is exists or not.
 //if (!System.IO.File.Exists(@"\UNCSharedCustomErrors404.es-MX.htm"))
 // log error if file path doesn't exists

 isChanged = true;
 break;
 }
}

if (isChanged) {
 siteRootDE.CommitChanges();
}

The same way you can also set other web site related properties.

Please leave a comment or question if you have any.

Posted in Microsoft Technology Tagged with:
2 comments on “Programmatically Change HTTPErrors in IIS6
  1. Mozes Andrianto says:

    Hi, do you know how to convert a virtual directory to an application in iis 6.0? I need it. thank you

  2. daigoumee says:

    Pretty nice post. I just stumbled upon your blog and wanted to say that I have really enjoyed browsing your blog posts. In any case I’ll be subscribing to your feed and I hope you write again soon!

Ads