Example #1
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));
        }
Example #2
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));
        }
Example #3
0
 /// <summary>
 /// Adds a new permanent 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>
 /// <returns>An instance of <see cref="RedirectItem"/> representing the created redirect.</returns>
 public RedirectItem AddRedirect(int rootNodeId, string url, RedirectLinkItem destionation)
 {
     return(AddRedirect(rootNodeId, url, destionation, true, false, false));
 }