public ActionResult Create(ConfigModel model)
        {
            using (var serverManager = new ServerManager(Environment.ExpandEnvironmentVariables(@"%APP_POOL_CONFIG%")))
            {
                var path = @"Web.config";

                var sites = serverManager.Sites;

                var webConfig = serverManager.GetWebConfiguration(webSiteName);
                string rulesSection = "rules";
                var inboundRulesCollection =
                    webConfig.GetSection("system.webServer/rewrite/" + rulesSection).GetCollection();
                var outboundRulesSection = webConfig.GetSection("system.webServer/rewrite/outboundRules");
                var outbBoundRulesCollection = outboundRulesSection.GetCollection();
                var preConditionsCollection = outboundRulesSection.GetCollection("preConditions");

                inboundRulesCollection.AddAt(inboundRulesCollection.Count - 1,
                CreateInboundGwRemoteUserRule(model, inboundRulesCollection.CreateElement("rule")));

                serverManager.CommitChanges();
            }

            return RedirectToAction("Index");
        }
        public ActionResult Edit(ConfigModel model)
        {
            using (var serverManager = new ServerManager(Environment.ExpandEnvironmentVariables(@"%APP_POOL_CONFIG%")))
            {
                var path = @"Web.config";

                var sites = serverManager.Sites;

                var webConfig = serverManager.GetWebConfiguration(webSiteName);
                string rulesSection = "rules";
                var inboundRulesCollection =
                    webConfig.GetSection("system.webServer/rewrite/" + rulesSection).GetCollection();
                var outboundRulesSection = webConfig.GetSection("system.webServer/rewrite/outboundRules");
                var outboundRulesCollection = outboundRulesSection.GetCollection();
                var preConditionsCollection = outboundRulesSection.GetCollection("preConditions");

                var rule = inboundRulesCollection.First(
                    r => r.GetAttribute("name").Value.ToString() == model.RuleName);

                var conditions = rule.GetCollection("conditions");
                var condition = conditions.FirstOrDefault(c => c.GetAttribute("input").Value.ToString() == "{HTTP_HOST}");

                var action = rule.GetChildElement("action");
                action.GetAttribute("url").Value = model.UrlRewrite;

                condition.GetAttribute("pattern").Value = model.Pattern;

                serverManager.CommitChanges();
            }
            return View();
        }
        private static ConfigModel GetConfigModelFromId(string id)
        {
            ConfigModel model;
            using (var serverManager = new ServerManager(Environment.ExpandEnvironmentVariables(@"%APP_POOL_CONFIG%")))
            {
                var path = @"Web.config";

                var sites = serverManager.Sites;

                var webConfig = serverManager.GetWebConfiguration(webSiteName);
                string rulesSection = "rules";
                var inboundRulesCollection =
                    webConfig.GetSection("system.webServer/rewrite/" + rulesSection).GetCollection();
                var outboundRulesSection = webConfig.GetSection("system.webServer/rewrite/outboundRules");
                var outboundRulesCollection = outboundRulesSection.GetCollection();
                var preConditionsCollection = outboundRulesSection.GetCollection("preConditions");

                var rule = inboundRulesCollection.First(
                    r => r.GetAttribute("name").Value.ToString() == id);

                var conditions = rule.GetCollection("conditions");
                var condition = conditions.FirstOrDefault(c => c.GetAttribute("input").Value.ToString() == "{HTTP_HOST}");

                var action = rule.GetChildElement("action");

                model = new ConfigModel()
                {
                    Pattern = condition != null ? condition.GetAttribute("pattern").Value.ToString() : string.Empty,
                    RuleName = id,
                    UrlRewrite = action.GetAttributeValue("url").ToString()
                };
            }
            return model;
        }
        private static ConfigurationElement CreateInboundGwRemoteUserRule(ConfigModel model, ConfigurationElement ruleElement)
        {
            ruleElement["name"] = model.RuleName;
            ruleElement["patternSyntax"] = "ECMAScript";
            ruleElement["stopProcessing"] = true;

            var matchElement = ruleElement.GetChildElement("match");
            matchElement["url"] = "(.*)";

            var conditionsElement = ruleElement.GetChildElement("conditions");

            var conditionsCollection = conditionsElement.GetCollection();

            //Get the origin requested :
            var inputElement = conditionsCollection.CreateElement("add");
            inputElement["input"] = "{" + ServerVariable.SERVER_VARIABLE_HTTP_HOST + "}";
            inputElement["pattern"] = model.Pattern;
            conditionsCollection.Add(inputElement);

            string arrServer = Environment.ExpandEnvironmentVariables(@"%APPSETTING_WEBSITE_SITE_NAME%");
            // set the server variable
            var variablesCollection = ruleElement.GetChildElement("serverVariables").GetCollection();
            AddServerVariable(variablesCollection, ServerVariable.SERVER_VARIABLE_HTTP_X_UNPROXIED_URL, arrServer + "{R:1}");
            AddServerVariable(variablesCollection, ServerVariable.SERVER_VARIABLE_HTTP_X_ORIGINAL_ACCEPT_ENCODING, "{" + ServerVariable.SERVER_VARIABLE_ACCEPT_ENCODING + "}");
            AddServerVariable(variablesCollection, ServerVariable.SERVER_VARIABLE_HTTP_X_ORIGINAL_HOST, "{" + ServerVariable.SERVER_VARIABLE_HTTP_HOST + "}");
            AddServerVariable(variablesCollection, ServerVariable.SERVER_VARIABLE_ACCEPT_ENCODING, "");
            AddServerVariable(variablesCollection, ServerVariable.SERVER_VARIABLE_HTTP_REFERER, model.UrlRewrite + "/{R:1}");

            var actionElement = ruleElement.GetChildElement("action");
            actionElement["type"] = "Rewrite";
            actionElement["url"] = model.UrlRewrite+"/{R:1}";

            return ruleElement;
        }