Microsoft.Web.Administration.dll Scripting IIS 7.0 on Vista

This is not a full article on how to script IIS7.0.  It’s about getting to that point on how to do it.  And as per usual the internet was full of incomplete information.  Here’s my complete solution when I faced problems.

Firstly the namespace Microsoft.Web.Administration was not available, nor was it in the .NET of the “Add reference” menu option.  To solve it I had to browse to:


and reference the


in that folder.  The namespace was available, and so I was rocking…straight into another brick wall!  What these other websites again forget to tell you is to set permissions on a specific file that the code I will give in a second, needs to have to be able to run.  So the file is found here:


and you need to give it the ASP.NET/NETWORK SERVICE user in security with READ access.  But guess what!?  Yes that still didn’t solve the issue!  I had to go into IIS7.0, choose the Application Pool I was working with, and change the Identity value to LocalSystem.  So thats:

Open IIS > Expand IIS > Application Pools > [select your application pool] > Advanced Settings > Process Model > Identity = LocalSystem

Job done.  Yes at last!  Woo!  I could then add new Rewrite rules into the web.config without having to manually go into IIS!

The code as promised

using System;
using Microsoft.Web.Administration;

public partial class new_iis : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)
        using (ServerManager serverManager = new ServerManager())
            Configuration config = serverManager.GetWebConfiguration("HIE");

            ConfigurationSection rulesSection = config.GetSection("system.webServer/rewrite/rules");
            ConfigurationElementCollection rulesCollection = rulesSection.GetCollection();

            ConfigurationElement ruleElement = rulesCollection.CreateElement("rule");
            ruleElement["name"] = @"GoToGoogle2";
            ruleElement["stopProcessing"] = true;

            ConfigurationElement matchElement = ruleElement.GetChildElement("match");
            matchElement["url"] = @"^google$";

            ConfigurationElement conditionsElement = ruleElement.GetChildElement("conditions");
            conditionsElement["logicalGrouping"] = "MatchAll";
            conditionsElement["trackAllCaptures"] = "false";

            ConfigurationElement actionElement = ruleElement.GetChildElement("action");
            actionElement["type"] = @"Redirect";
            actionElement["url"] = @"";

            rulesCollection.AddAt(0, ruleElement);


But I never wrote the code. I found it at so I have to massively thank them for showing how easy it is.  And remember you will be able to edit any part of the web.config via this code, or similar code ’cause you will need to edit it to your needs.

Leave a Reply

Your email address will not be published.