Exemplo n.º 1
0
        public object EditRedirect(int rootNodeId, string redirectId, string url, string linkMode, int linkId, string linkUrl, string linkName = null, bool permanent = true, bool regex = false, bool forward = false)
        {
            try {
                // Get a reference to the redirect
                RedirectItem redirect = _redirects.GetRedirectById(redirectId);
                if (redirect == null)
                {
                    throw new RedirectNotFoundException();
                }

                // Some input validation
                if (string.IsNullOrWhiteSpace(url))
                {
                    throw new RedirectsException(Localize("redirects/errorNoUrl"));
                }
                if (string.IsNullOrWhiteSpace(linkUrl))
                {
                    throw new RedirectsException(Localize("redirects/errorNoDestination"));
                }
                if (string.IsNullOrWhiteSpace(linkMode))
                {
                    throw new RedirectsException(Localize("redirects/errorNoDestination"));
                }

                // Parse the link mode
                RedirectLinkMode mode;
                switch (linkMode)
                {
                case "content": mode = RedirectLinkMode.Content; break;

                case "media": mode = RedirectLinkMode.Media; break;

                case "url": mode = RedirectLinkMode.Url; break;

                default: throw new RedirectsException(Localize("redirects/errorUnknownLinkMode"));
                }

                // Initialize a new link item
                RedirectLinkItem destination = new RedirectLinkItem(linkId, linkName, linkUrl, mode);

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

                // Update the properties of the redirect
                redirect.RootId             = rootNodeId;
                redirect.Url                = url;
                redirect.QueryString        = query;
                redirect.Link               = destination;
                redirect.IsPermanent        = permanent;
                redirect.IsRegex            = regex;
                redirect.ForwardQueryString = forward;

                // Save/update the redirect
                _redirects.SaveRedirect(redirect);

                // Return the redirect
                return(redirect);
            } catch (RedirectsException ex) {
                // Generate the error response
                return(Request.CreateResponse(JsonMetaResponse.GetError(HttpStatusCode.InternalServerError, ex.Message)));
            }
        }