コード例 #1
0
        /// <summary>
        /// Remove all rewrite rules that start with the given prefix.
        /// </summary>
        /// <param name="prefix"></param>
        /// <param name="logger"></param>
        public void RemoveRewriteRulesWithPrefix(string prefix, ILoggerInterface logger)
        {
            // If there is no CDN site, do nothing
            using (ServerManager manager = new ServerManager())
            {
                var site = UtilsIis.FindSiteWithName(manager, this.CstChefCndSiteName, logger).SingleOrDefault();

                if (site == null)
                {
                    return;
                }
            }

            var       webconfigfilepath = this.GetCdnWebConfigPathInitialized();
            XDocument webcfg            = XDocument.Parse(File.ReadAllText(webconfigfilepath));

            var rules = (from p in webcfg.Descendants("rule")
                         where p.Attribute("name")?.Value?.StartsWith(prefix) == true
                         select p).ToList();

            foreach (var rule in rules)
            {
                rule?.Remove();
            }

            UtilsIis.WriteWebConfig(webconfigfilepath, webcfg.ToString());
        }
コード例 #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="rule">The rule</param>
        /// <param name="replaceIfExists">If a rule exists with the same name, replace it, otherwise, throw exception.</param>
        public void AddRewriteRule(string rule, bool replaceIfExists = true)
        {
            var parsedRule = XElement.Parse(rule);

            string name = parsedRule.DescendantsAndSelf("rule").First().Attribute("name").Value;

            var       webconfigfilepath = this.GetCdnWebConfigPathInitialized();
            XDocument webcfg            = XDocument.Parse(File.ReadAllText(webconfigfilepath));

            XElement ruleselement = webcfg.Root.GetAndEnsureXpath("system.webServer/rewrite/rules");

            var existingRule = (from p in ruleselement.Descendants("rule")
                                where p.Attribute("name")?.Value == name
                                select p).FirstOrDefault();

            if (existingRule != null && !replaceIfExists)
            {
                throw new Exception("Rule already exists");
            }

            existingRule?.Remove();

            // Now add from scratchrule
            ruleselement.Add(parsedRule);

            // Perist it
            UtilsIis.WriteWebConfig(webconfigfilepath, webcfg.ToString());
        }
コード例 #3
0
        /// <summary>
        /// Move a rule to the requested INDEX in the set of rules
        /// </summary>
        /// <param name="ruleName"></param>
        /// <param name="index"></param>
        protected void SetRuleIndex(string ruleName, int index)
        {
            var webconfigfilepath = this.GetCdnWebConfigPathInitialized();

            XDocument webcfg = XDocument.Parse(File.ReadAllText(webconfigfilepath));

            XElement ruleselement = webcfg.Root.GetAndEnsureXpath("system.webServer/rewrite/rules");

            XElement existingRule = null;

            var nodes = ruleselement.Nodes().ToList();

            foreach (var node in nodes.ToList())
            {
                if (node is XElement xNode && xNode.Attribute("name")?.Value == ruleName)
                {
                    if (nodes.IndexOf(xNode) == index)
                    {
                        return;
                    }

                    existingRule = xNode;
                    nodes.Remove(xNode);
                    break;
                }
            }

            if (existingRule == null)
            {
                throw new Exception($"Rule {ruleName} not found.");
            }

            nodes.Insert(index, existingRule);

            ruleselement.ReplaceAll(nodes.ToArray());

            UtilsIis.WriteWebConfig(webconfigfilepath, webcfg.ToString());
        }