コード例 #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="RedirectDestination"/> 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, RedirectDestination 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.Type,
                Url                = url,
                QueryString        = query,
                Created            = DateTime.Now,
                Updated            = DateTime.Now,
                IsPermanent        = permanent,
                IsRegex            = isRegex,
                ForwardQueryString = forwardQueryString
            };

            using (var scope = _scopeProvider.CreateScope()) {
                var database = scope.Database;

                try {
                    // Attempt to add the redirect to the database
                    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");
                }
                scope.Complete();
            }

            // Make the call to the database
            return(GetRedirectById(item.Id));
        }
コード例 #2
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="RedirectDestination"/> representing the destination link.</param>
 /// <returns>An instance of <see cref="RedirectItem"/> representing the created redirect.</returns>
 public RedirectItem AddRedirect(int rootNodeId, string url, RedirectDestination destionation)
 {
     return(AddRedirect(rootNodeId, url, destionation, true, false, false));
 }