Example #1
0
        public ActionResult EditRedirect(Guid redirectId, [FromBody] EditRedirectOptions model)
        {
            try {
                // Get a reference to the redirect
                IRedirect redirect = _redirects.GetRedirectByKey(redirectId);
                if (redirect == null)
                {
                    throw new RedirectNotFoundException();
                }

                // Some input validation
                if (model == null)
                {
                    throw new RedirectsException("Failed parsing request body.");
                }
                if (string.IsNullOrWhiteSpace(model.OriginalUrl))
                {
                    throw new RedirectsException(_backOffice.Localize("errorNoUrl"));
                }
                if (string.IsNullOrWhiteSpace(model.Destination?.Url))
                {
                    throw new RedirectsException(_backOffice.Localize("errorNoDestination"));
                }

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

                redirect.RootKey            = model.RootNodeKey;
                redirect.Url                = url;
                redirect.QueryString        = query;
                redirect.Destination        = model.Destination;
                redirect.IsPermanent        = model.IsPermanent;
                redirect.ForwardQueryString = model.ForwardQueryString;

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

                // Map the result for the API
                return(new JsonResult(_backOffice.Map(redirect)));
            } catch (RedirectsException ex) {
                if (!ex.Is404)
                {
                    _logger.LogError(ex, ex.Message);
                }

                // Generate the error response
                return(Error(ex));
            }
        }
        public object EditRedirect(Guid redirectId, [FromBody] EditRedirectOptions model)
        {
            try {
                // Get a reference to the redirect
                RedirectItem redirect = _redirects.GetRedirectByKey(redirectId);
                if (redirect == null)
                {
                    throw new RedirectNotFoundException();
                }

                // Some input validation
                if (string.IsNullOrWhiteSpace(model.OriginalUrl))
                {
                    throw new RedirectsException(Localize("redirects/errorNoUrl"));
                }
                if (string.IsNullOrWhiteSpace(model.Destination?.Url))
                {
                    throw new RedirectsException(Localize("redirects/errorNoDestination"));
                }

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

                redirect.RootId             = model.RootNodeId;
                redirect.RootKey            = model.RootNodeKey;
                redirect.Url                = url;
                redirect.QueryString        = query;
                redirect.LinkId             = model.Destination.Id;
                redirect.LinkKey            = model.Destination.Key;
                redirect.LinkUrl            = model.Destination.Url;
                redirect.LinkMode           = model.Destination.Type;
                redirect.IsPermanent        = model.IsPermanent;
                redirect.ForwardQueryString = model.ForwardQueryString;

                // 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)));
            }
        }
Example #3
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)));
            }
        }