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