public object AddRedirect(int rootNodeId, string url, string linkMode, int linkId, string linkUrl, string linkName = null)
        {
            try {
                RedirectLinkItem redirect = RedirectLinkItem.Parse(new JObject {
                    { "id", linkId },
                    { "name", linkName + "" },
                    { "url", linkUrl },
                    { "mode", linkMode }
                });

                return(Repository.AddRedirect(rootNodeId, url, redirect));
            } catch (RedirectsException ex) {
                return(Request.CreateResponse(JsonMetaResponse.GetError(HttpStatusCode.InternalServerError, ex.Message)));
            }
        }
        public object EditRedirect(int rootNodeId, string redirectId, string url, string linkMode, int linkId, string linkUrl, string linkName = null)
        {
            try {
                // Get the redirect from the database
                RedirectItem redirect = Repository.GetRedirectById(redirectId);
                if (redirect == null)
                {
                    throw new RedirectNotFoundException();
                }

                if (String.IsNullOrWhiteSpace(url))
                {
                    throw new RedirectsException("You must specify a URL for the redirect.");
                }
                if (String.IsNullOrWhiteSpace(linkUrl))
                {
                    throw new RedirectsException("You must specify a destination link for the redirect.");
                }
                if (String.IsNullOrWhiteSpace(linkMode))
                {
                    throw new RedirectsException("You must specify a destination link for the redirect.");
                }

                // Initialize a new link picker item
                RedirectLinkItem link = RedirectLinkItem.Parse(new JObject {
                    { "id", linkId },
                    { "name", linkName + "" },
                    { "url", linkUrl },
                    { "mode", linkMode }
                });

                string[] urlParts = url.Split('?');
                url = urlParts[0].TrimEnd('/');
                string query = urlParts.Length == 2 ? urlParts[1] : "";

                redirect.RootNodeId  = rootNodeId;
                redirect.Url         = url;
                redirect.QueryString = query;
                redirect.Link        = link;

                Repository.SaveRedirect(redirect);

                return(redirect);
            } catch (RedirectsException ex) {
                return(Request.CreateResponse(JsonMetaResponse.GetError(HttpStatusCode.InternalServerError, ex.Message)));
            }
        }