コード例 #1
0
        /// <summary>
        /// Deletes the specified <paramref name="redirect"/> from the database.
        /// </summary>
        /// <param name="redirect">The redirected to be deleted.</param>
        public void DeleteRedirect(RedirectItem redirect)
        {
            // Some input validation
            if (redirect == null)
            {
                throw new ArgumentNullException(nameof(redirect));
            }


            using (var scope = WebComposing.Current.ScopeProvider.CreateScope())
            {
                var database = scope.Database;

                try
                {
                    // Remove the redirect from the database
                    database.Delete(redirect.Row);
                }
                catch (Exception ex)
                {
                    WebComposing.Current.Logger.Error <RedirectsRepository>("Unable to delete redirect from database", ex);
                    throw new Exception("Unable to delete redirect from database");
                }
                scope.Complete();
            }
        }
コード例 #2
0
        /// <summary>
        /// Saves the specified <paramref name="redirect"/>.
        /// </summary>
        /// <param name="redirect">The redirected to be saved.</param>
        /// <returns>The saved <paramref name="redirect"/>.</returns>
        public RedirectItem SaveRedirect(RedirectItem redirect)
        {
            // Some input validation
            if (redirect == null)
            {
                throw new ArgumentNullException(nameof(redirect));
            }

            // Check whether another redirect matches the new URL and query string
            RedirectItem existing = GetRedirectByUrl(redirect.RootId, redirect.Url, redirect.QueryString);

            if (existing != null && existing.Id != redirect.Id)
            {
                throw new RedirectsException("A redirect with the same URL and query string already exists.");
            }

            // Update the timestamp for when the redirect was modified
            redirect.Updated = DateTime.Now;

            // Update the redirect in the database
            using (var scope = _scopeProvider.CreateScope()) {
                try {
                    scope.Database.Update(redirect.Dto);
                } catch (Exception ex) {
                    _logger.Error <RedirectsService>("Unable to update redirect into the database", ex);
                    throw new Exception("Unable to update redirect into the database", ex);
                }
                scope.Complete();
            }

            return(redirect);
        }
コード例 #3
0
        /// <summary>
        /// Adds a new redirect matching the specified inbound <paramref name="url"/>. A request to
        /// <paramref name="url"/> will automatically be redirected to the URL of the specified
        /// <paramref name="destionation"/> link.
        /// </summary>
        /// <param name="rootNodeId">THe ID of the root/side node. Use <c>0</c> for a global redirect.</param>
        /// <param name="url">The inbound URL to match.</param>
        /// <param name="destionation">An instance of <see cref="RedirectLinkItem"/> representing the destination link.</param>
        /// <param name="permanent">Whether the redirect should be permanent (301) or temporary (302).</param>
        /// <param name="isRegex">Whether regex should be enabled for the redirect.</param>
        /// <param name="forwardQueryString">Whether the redirect should forward the original query string.</param>
        /// <returns>An instance of <see cref="RedirectItem"/> representing the created redirect.</returns>
        public RedirectItem AddRedirect(int rootNodeId, string url, RedirectLinkItem destionation, bool permanent, bool isRegex, bool forwardQueryString)
        {
            // Attempt to create the database table if it doesn't exist
            //try {
            if (!SchemaHelper.TableExist(RedirectItemRow.TableName))
            {
                SchemaHelper.CreateTable <RedirectItemRow>(false);
            }
            //} catch (Exception ex) {
            //    LogHelper.Error<RedirectsRepository>("Unable to create database table: " + RedirectItem.TableName, ex);
            //    throw new Exception("Din opgave kunne ikke oprettes pga. en fejl på serveren");
            //}

            var query = "";

            if (!isRegex)
            {
                string[] urlParts = url.Split('?');
                url   = urlParts[0].TrimEnd('/');
                query = urlParts.Length == 2 ? urlParts[1] : "";
            }

            if (GetRedirectByUrl(rootNodeId, url, query) != null)
            {
                throw new RedirectsException("A redirect with the specified URL already exists.");
            }

            // Initialize the new redirect and populate the properties
            RedirectItem item = new RedirectItem {
                RootNodeId         = rootNodeId,
                LinkId             = destionation.Id,
                LinkUrl            = destionation.Url,
                LinkMode           = destionation.Mode,
                LinkName           = destionation.Name,
                Url                = url,
                QueryString        = query,
                Created            = DateTime.Now,
                Updated            = DateTime.Now,
                IsPermanent        = permanent,
                IsRegex            = isRegex,
                ForwardQueryString = forwardQueryString
            };

            // Attempt to add the redirect to the database
            try {
                Database.Insert(item.Row);
            } catch (Exception ex) {
                LogHelper.Error <RedirectsRepository>("Unable to insert redirect into the database", ex);
                throw new Exception("Unable to insert redirect into the database");
            }

            // Make the call to the database
            return(GetRedirectById(item.Id));
        }
コード例 #4
0
        /// <summary>
        /// Deletes the specified <paramref name="redirect"/> from the database.
        /// </summary>
        /// <param name="redirect">The redirected to be deleted.</param>
        public void DeleteRedirect(RedirectItem redirect)
        {
            // Some input validation
            if (redirect == null)
            {
                throw new ArgumentNullException(nameof(redirect));
            }

            // Remove the redirect from the database
            Database.Delete(redirect.Row);
        }
コード例 #5
0
        public RedirectItem AddRedirect(AddRedirectOptions model)
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }

            string url   = model.OriginalUrl;
            string query = string.Empty;

            if (model.IsRegex == false)
            {
                string[] urlParts = url.Split('?');
                url   = urlParts[0].TrimEnd('/');
                query = urlParts.Length == 2 ? urlParts[1] : string.Empty;
            }

            if (GetRedirectByUrl(model.RootNodeId, url, query) != null)
            {
                throw new RedirectsException("A redirect with the specified URL already exists.");
            }

            // Initialize the new redirect and populate the properties
            RedirectItem item = new RedirectItem {
                RootId             = model.RootNodeId,
                RootKey            = model.RootNodeKey,
                LinkId             = model.Destination.Id,
                LinkKey            = model.Destination.Key,
                LinkUrl            = model.Destination.Url,
                LinkMode           = model.Destination.Type,
                Url                = url,
                QueryString        = query,
                Created            = EssentialsTime.UtcNow,
                Updated            = EssentialsTime.UtcNow,
                IsPermanent        = model.IsPermanent,
                IsRegex            = model.IsRegex,
                ForwardQueryString = model.ForwardQueryString
            };

            // 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 Exception("Unable to insert redirect into the database", ex);
                }
                scope.Complete();
            }

            // Make the call to the database
            return(GetRedirectById(item.Id));
        }
コード例 #6
0
        /// <summary>
        /// Adds a new redirect matching the specified inbound <paramref name="url"/>. A request to
        /// <paramref name="url"/> will automatically be redirected to the URL of the specified
        /// <paramref name="destionation"/> link.
        /// </summary>
        /// <param name="rootNodeId">THe ID of the root/side node. Use <c>0</c> for a global redirect.</param>
        /// <param name="url">The inbound URL to match.</param>
        /// <param name="destionation">An instance of <see cref="RedirectLinkItem"/> representing the destination link.</param>
        /// <param name="permanent">Whether the redirect should be permanent (301) or temporary (302).</param>
        /// <param name="isRegex">Whether regex should be enabled for the redirect.</param>
        /// <param name="forwardQueryString">Whether the redirect should forward the original query string.</param>
        /// <returns>An instance of <see cref="RedirectItem"/> representing the created redirect.</returns>
        public RedirectItem AddRedirect(int rootNodeId, string url, RedirectLinkItem destionation, bool permanent, bool isRegex, bool forwardQueryString)
        {
            var query = "";

            if (!isRegex)
            {
                string[] urlParts = url.Split('?');
                url   = urlParts[0].TrimEnd('/');
                query = urlParts.Length == 2 ? urlParts[1] : "";
            }

            if (GetRedirectByUrl(rootNodeId, url, query) != null)
            {
                throw new RedirectsException("A redirect with the specified URL already exists.");
            }

            // Initialize the new redirect and populate the properties
            RedirectItem item = new RedirectItem {
                RootId             = rootNodeId,
                LinkId             = destionation.Id,
                LinkUrl            = destionation.Url,
                LinkMode           = destionation.Mode,
                LinkName           = destionation.Name,
                Url                = url,
                QueryString        = query,
                Created            = DateTime.Now,
                Updated            = DateTime.Now,
                IsPermanent        = permanent,
                IsRegex            = isRegex,
                ForwardQueryString = forwardQueryString
            };

            using (var scope = WebComposing.Current.ScopeProvider.CreateScope())
            {
                var database = scope.Database;

                try
                {
                    // Attempt to add the redirect to the database
                    database.Insert(item.Row);
                }
                catch (Exception ex)
                {
                    WebComposing.Current.Logger.Error <RedirectsRepository>("Unable to insert redirect into the database", ex);
                    throw new Exception("Unable to insert redirect into the database");
                }
                scope.Complete();
            }

            // Make the call to the database
            return(GetRedirectById(item.Id));
        }
コード例 #7
0
        /// <summary>
        /// Deletes the specified <paramref name="redirect"/> from the database.
        /// </summary>
        /// <param name="redirect">The redirected to be deleted.</param>
        public void DeleteRedirect(RedirectItem redirect)
        {
            // Some input validation
            if (redirect == null)
            {
                throw new ArgumentNullException(nameof(redirect));
            }

            // Remove the redirect from the database
            using (var scope = _scopeProvider.CreateScope()) {
                try {
                    scope.Database.Delete(redirect.Dto);
                } catch (Exception ex) {
                    _logger.Error <RedirectsService>("Unable to delete redirect from database", ex);
                    throw new Exception("Unable to delete redirect from database", ex);
                }
                scope.Complete();
            }
        }
コード例 #8
0
        public string HandleForwardQueryString(RedirectItem redirect, string rawurl)
        {
            string newRedirectUrl;

            // find querystrings from rawurl
            string[] elementsRawurl     = rawurl.Split('?');
            string   querystringsRawurl = 1 < elementsRawurl.Length ? elementsRawurl[1] : null;

            if (!string.IsNullOrWhiteSpace(querystringsRawurl))
            {
                // we have querystrings in the original url

                // find querystrings in the redirect url
                string[] elementsRedirecturl     = redirect.LinkUrl.Split('?');
                string   querystringsRedirecturl = 1 < elementsRedirecturl.Length ? elementsRedirecturl[1] : null;

                // merge querystrings
                List <string> queryElements = new List <string>();

                if (!string.IsNullOrWhiteSpace(querystringsRedirecturl))
                {
                    queryElements.Add(querystringsRedirecturl);
                }

                queryElements.Add(querystringsRawurl);

                // create new redirect url w. merged querystrings
                newRedirectUrl = $"{elementsRedirecturl[0]}?{string.Join("&", queryElements)}";
            }
            else
            {
                newRedirectUrl = redirect.LinkUrl;
            }

            return(newRedirectUrl);
        }
コード例 #9
0
        /// <summary>
        /// Saves the specified <paramref name="redirect"/>.
        /// </summary>
        /// <param name="redirect">The redirected to be saved.</param>
        /// <returns>The saved <paramref name="redirect"/>.</returns>
        public RedirectItem SaveRedirect(RedirectItem redirect)
        {
            // Some input validation
            if (redirect == null)
            {
                throw new ArgumentNullException(nameof(redirect));
            }

            // Check whether another redirect matches the new URL and query string
            RedirectItem existing = GetRedirectByUrl(redirect.RootNodeId, redirect.Url, redirect.QueryString);

            if (existing != null && existing.Id != redirect.Id)
            {
                throw new RedirectsException("A redirect with the same URL and query string already exists.");
            }

            // Update the timestamp for when the redirect was modified
            redirect.Updated = DateTime.Now;

            // Update the redirect in the database
            Database.Update(redirect.Row);

            return(redirect);
        }