public object Patch([FromBody] dynamic model, string id)
        {
            var inboundRuleId = new InboundRuleId(id);

            Site site = inboundRuleId.SiteId == null ? null : SiteHelper.GetSite(inboundRuleId.SiteId.Value);

            if (inboundRuleId.SiteId != null && site == null)
            {
                return(NotFound());
            }

            InboundRulesSection section = InboundRulesHelper.GetSection(site, inboundRuleId.Path);
            InboundRule         rule    = (InboundRule)section.InboundRules.FirstOrDefault(r => r.Name.Equals(inboundRuleId.Name, StringComparison.OrdinalIgnoreCase));

            if (rule == null)
            {
                return(NotFound());
            }

            InboundRulesHelper.UpdateRule(model, rule, site, inboundRuleId.Path, ManagementUnit.ResolveConfigScope(model));

            ManagementUnit.Current.Commit();

            dynamic updatedRule = InboundRulesHelper.RuleToJsonModel(rule, site, inboundRuleId.Path, Context.Request.GetFields(), true);

            if (updatedRule.id != id)
            {
                return(LocationChanged(InboundRulesHelper.GetRuleLocation(updatedRule.id), updatedRule));
            }

            return(updatedRule);
        }
        public object Post([FromBody] dynamic model)
        {
            if (model == null)
            {
                throw new ApiArgumentException("model");
            }

            RewriteId parentId = RewriteHelper.GetRewriteIdFromBody(model);

            if (parentId == null)
            {
                throw new ApiArgumentException("url_rewrite");
            }

            // Get site the rule is for if applicable
            Site site = parentId.SiteId == null ? null : SiteHelper.GetSite(parentId.SiteId.Value);

            string configPath           = ManagementUnit.ResolveConfigScope(model);
            InboundRulesSection section = InboundRulesHelper.GetSection(site, parentId.Path, configPath);

            // Create rule
            InboundRule rule = InboundRulesHelper.CreateRule(model, site, parentId.Path, ManagementUnit.ResolveConfigScope(model));

            // Add it
            InboundRulesHelper.AddRule(rule, section, model);

            // Save
            ManagementUnit.Current.Commit();

            //
            // Create response
            dynamic r = InboundRulesHelper.RuleToJsonModel(rule, site, parentId.Path, Context.Request.GetFields(), true);

            return(Created(InboundRulesHelper.GetRuleLocation(r.id), r));
        }
        public object Get(string id)
        {
            var inboundRuleId = new InboundRuleId(id);

            Site site = inboundRuleId.SiteId == null ? null : SiteHelper.GetSite(inboundRuleId.SiteId.Value);

            if (inboundRuleId.SiteId != null && site == null)
            {
                return(NotFound());
            }

            InboundRule rule = (InboundRule)InboundRulesHelper.GetSection(site, inboundRuleId.Path).InboundRules.FirstOrDefault(r => r.Name.Equals(inboundRuleId.Name, StringComparison.OrdinalIgnoreCase));

            if (rule == null)
            {
                return(NotFound());
            }

            return(InboundRulesHelper.RuleToJsonModel(rule, site, inboundRuleId.Path, Context.Request.GetFields()));
        }