/// <inheritdoc /> public IRedirect AddRedirect(AddRedirectOptions options) { if (options == null) { throw new ArgumentNullException(nameof(options)); } string url = options.OriginalUrl; string query = string.Empty; if (GetRedirectByPathAndQuery(options.RootNodeKey, url, query) != null) { throw new RedirectsException("A redirect with the specified URL already exists."); } // Initialize the destination RedirectDestination destination = new RedirectDestination { Id = options.Destination.Id, Key = options.Destination.Key, Name = options.Destination.Name, Url = options.Destination.Url, Query = options.Destination.Query ?? string.Empty, Fragment = options.Destination.Fragment ?? string.Empty, Type = options.Destination.Type }; // Initialize the new redirect and populate the properties Redirect item = new Redirect { RootKey = options.RootNodeKey, Url = url, QueryString = query, CreateDate = EssentialsTime.UtcNow, UpdateDate = EssentialsTime.UtcNow, IsPermanent = options.IsPermanent, ForwardQueryString = options.ForwardQueryString }.SetDestination(destination); // Attempt to add the redirect to the database using (IScope scope = _scopeProvider.CreateScope()) { try { scope.Database.Insert(item.Dto); } catch (Exception ex) { //_logger.Error<RedirectsService>("Unable to insert redirect into the database", ex); throw new RedirectsException("Unable to insert redirect into the database.", ex); } scope.Complete(); } // Make the call to the database return(GetRedirectById(item.Id)); }
public object AddRedirect(int rootNodeId, string url, string linkMode, int linkId, Guid linkKey, string linkUrl, bool permanent = true, bool regex = false, bool forward = false) { try { // 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 RedirectDestinationType mode; switch (linkMode) { case "content": mode = RedirectDestinationType.Content; break; case "media": mode = RedirectDestinationType.Media; break; case "url": mode = RedirectDestinationType.Url; break; default: throw new RedirectsException(Localize("redirects/errorUnknownLinkMode")); } // Initialize a new link item RedirectDestination destination = new RedirectDestination(linkId, linkKey, linkUrl, mode); // Add the redirect RedirectItem redirect = _redirects.AddRedirect(rootNodeId, url, destination, permanent, regex, forward); // Return the redirect return(redirect); } catch (RedirectsException ex) { // Generate the error response return(Request.CreateResponse(JsonMetaResponse.GetError(HttpStatusCode.InternalServerError, ex.Message))); } }
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)); } }
/// <summary> /// Initializes a new instance based on the specified <see cref="JObject"/>. /// </summary> /// <param name="obj">An instance of <see cref="JObject"/> representing the redirect.</param> protected OutboundRedirect(JObject obj) : base(obj) { IsPermanent = obj.GetBoolean("permanent"); Destination = obj.GetObject("destination", RedirectDestination.Parse) ?? new RedirectDestination(); }
/// <summary> /// Initializes a new instance with an empty model. /// </summary> public OutboundRedirect() : base(null) { IsPermanent = true; Destination = new RedirectDestination(); }
public object EditRedirect(int rootNodeId, Guid redirectId, string url, string linkMode, int linkId, Guid linkKey, string linkUrl, bool permanent = true, bool regex = false, bool forward = false) { try { // Get a reference to the redirect RedirectItem redirect = _redirects.GetRedirectByKey(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 RedirectDestinationType mode; switch (linkMode) { case "content": mode = RedirectDestinationType.Content; break; case "media": mode = RedirectDestinationType.Media; break; case "url": mode = RedirectDestinationType.Url; break; default: throw new RedirectsException(Localize("redirects/errorUnknownLinkMode")); } // Initialize a new link item RedirectDestination destination = new RedirectDestination(linkId, linkKey, linkUrl, mode); // 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.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))); } }
/// <summary> /// Initializes a new instance based on the specified <see cref="JObject"/>. /// </summary> /// <param name="obj">An instance of <see cref="JObject"/> representing the redirect.</param> protected OutboundRedirect(JObject obj) : base(obj) { IsPermanent = obj.GetBoolean("permanent"); Link = obj.GetObject(obj.HasValue("items") ? "items.items[0]" : "link", RedirectDestination.Parse) ?? new RedirectDestination(); }