Esempio n. 1
0
        public ActionResult EditRedirect(Guid rootNodeKey, Guid redirectId, string url,
                                         string linkMode, int linkId, Guid linkKey, string linkUrl,
                                         bool permanent = true, bool forward = false)
        {
            try {
                // Get a reference to the redirect
                IRedirect redirect = _redirects.GetRedirectByKey(redirectId);
                if (redirect == null)
                {
                    throw new RedirectNotFoundException();
                }

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

                // Parse the destination type
                RedirectDestinationType type;
                switch (linkMode)
                {
                case "content": type = RedirectDestinationType.Content; break;

                case "media": type = RedirectDestinationType.Media; break;

                case "url": type = RedirectDestinationType.Url; break;

                default: throw new RedirectsException(_backOffice.Localize("errorUnknownLinkType"));
                }

                // Initialize a new destination instance
                RedirectDestination destination = new RedirectDestination {
                    Id   = linkId,
                    Key  = linkKey,
                    Type = type,
                    Name = redirect.Destination?.Name
                };

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

                // Update the properties of the redirect
                redirect.RootKey     = rootNodeKey;
                redirect.Url         = url;
                redirect.QueryString = query;
                redirect.SetDestination(destination);
                redirect.IsPermanent        = permanent;
                redirect.ForwardQueryString = forward;

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